Search code examples
flutterdartjust-audio

having flutter Just audio player plugin having a source error


I'm creating this music player and I want to play audio in my playlist I'm using on_audio_query plugins to query all the audios specifically in my playlist folder that I created, and I'm using just_audio plugin as my player. but the problem is every time I try to play the songs it returns me an error saying source error

FutureBuilder<List<SongModel>>(
          future: _audioQuery.queryAudiosFrom(
              AudiosFromType.PLAYLIST, widget.playlistpath,
              ignoreCase: true),
          builder: (context, item) {
            if (item.data == null) {
              return const Text("Searching for audio");
            }
            if (item.data!.isEmpty) {
              return const Text("No music Audio found");
            }
            return ListView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemCount: item.data!.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text(item.data![index].title),
                    onTap: () async {
                      try {
                        debugPrint("${item.data![index].uri}");
                        await _player.setAudioSource(AudioSource.uri(
                            Uri.parse(item.data![index].uri!)));
                        // await _player.setAudioSource(
                        //     createPlaylist(item.data!),
                        //     initialIndex: index);
                        await _player.play();
                      } catch (e) {
                        debugPrint("$e");
                      }
                    },
                  );
                });
          }),

I/flutter (18513): (0) Source error


Solution

  • Bro I'm also facing this issue. I didn't got any solution. So what I did was, I just stored current song map item in a varialbe and just used that map item to play song in just_audio:

    void playSong() {
    
    Map map = widget.songModel.getMap;
    
    try{
      setState(() {
        isPlaying = true;
        indexno = widget.indexno;
        songId = widget.songModel.id;
        songName = widget.songModel.displayNameWOExt;
        artistName = widget.songModel.artist!;
      });
    
      audioPlayer.setAudioSource(
        AudioSource.uri(Uri.parse(map['_data']))
      );
    
      audioPlayer.play();
    
    } on Exception {
      log("cannot parse audio");
    }}
    

    Here map['_data'] will return exact location of audio.

    eg:- "/storage/6804-D450/music/ma music/music.mp3".

    You can also print the map variable to see other fields.