Home > Software engineering >  How can I get the details of currently playing song in miniplayer?
How can I get the details of currently playing song in miniplayer?

Time:12-19

I am working on a music player app in flutter. I fetch the songs from devices using on_audio_query package and display them in listview. When I click on a song, i set the plylist of all songs and play the one thats clicked using just_audio package like this.

                 await player.setAudioSource(
                  ConcatenatingAudioSource(
                    useLazyPreparation: true,
                    shuffleOrder: DefaultShuffleOrder(),
                    children: allSongsList! // allSongsList is the list of 
                     songs(on_audio_query)
                        .map((songModel) =>
                            AudioSource.uri(Uri.parse(songModel.data)))
                        .toList(),
                  ),
                  initialIndex: index, // Listview Index
                  initialPosition: Duration.zero);

                  await item.player.play();

I want to show miniplayer at the bottom only when there is a song playing(paused),refresh the song descriptions,how do I get the song description(Artist/song)?

CodePudding user response:

I will let you the implementation of the mini player widget for you, however, I will consider that I have a shouldMiniPlayerBeVisible, in the Scaffold of your page:

First, declare and initialize a shouldMiniPlayerBeVisible variable that will manage the show/hide of the mini player:

bool shouldMiniPlayerBeVisible = false;

and a currentAudio which will take the song and pass it in the mini player.

YourAudioModel currentAudio = YourAudioModel();

Here, I supposed that I have a simple YourAudioModel which will hold information about audio such as title, description, length, URL (or local) path ...

I recommend not setting it to null initially , you can initialize it with a placeholder YourAudioModel audio model that has some dummy data, or loading data information for example...

now in your Scaffold:

Scaffold(
floatingActionButton: Visibility(
  visible: shouldMiniPlayerBeVisible, 
  child: shouldMiniPlayerBeVisible(songToPlay: currentAudio),
  ),
),

I will consider that you are using a StatefulWidget, but the steps are the same as you need just to get the idea of it to implement it in other state management solutions.

in the initState, you need to listen to the playerStateStream stream like this:

player.playerStateStream.listen((state) {
 if (state.playing) {
  shouldMiniPlayerBeVisible = true;
 } else {
 shouldMiniPlayerBeVisible = false;
}
  setState(() {});
 });

this basically will track if any audio is playing or not, then show the mini player based on it.

now before your run the await item.player.play();, set your currentAudio variable to the new audio which is running, you can do it with a function like this:

Future<void> playCurrentAudioANdShowItInMiniPlayer(YourAudioModel audio) async {
currentAudio = YourAudioModel;
await item.player.play();
}

now from your UI, for each item in ListView, you can just call the playCurrentAudioANdShowItInMiniPlayer method with its YourAudioModeland expect it to work fine.

from here, you should implement the actual mini player widget, and you will need also a custom Navigator widget so that mini player will be on the screen even if you navigated to other screens, well I faced this issue before and the miniplayer worked just really fine and good as I expected, I recommend you to use it.

  • Related