Search code examples
flutterdartflutter-dependenciesaudio-playerjust-audio

Flutter how to get current duration time in assets_audio_player


I am previously using just_audio and I am taking the current time like this

player.positionStream.listen((event) {
  playerTimeNow = event;
  updatePlayerBar();
});

Now I need to know how can I get time in this assets_audio_player


Solution

  • What I understood from your question is that you want the current playing position of the audio and want to update your player bar. For that you can use currentPosition property of AssetAudioPlayer which returns ValueStream. Yon can either listen to the currentPosition Stream or use it in StreamBuilder.

    Simply listen to the currentPosition Stream :

     assetsAudioPlayer.currentPosition.listen((positionValue){
       playerTimeNow = positionValue;
      updatePlayerBar();
     });
    

    Or you can also use StreamBuilder:

    return StreamBuilder(
        stream: assetsAudioPlayer.currentPosition,
        builder: (context, asyncSnapshot) {
            final Duration duration = asyncSnapshot.data;
            return Text(duration.toString());  
        }),
    

    Wrap the Widget which you want to provide the current audio position in StreamBuilder and use the values received from asyncSnapshot.data.

    For Custom Notification :

    Use Audio to customize Notification :

    final audio = Audio("/assets/audio/country.mp3", 
        metas: Metas(
                title:  "Country",
                artist: "Florent Champigny",
                album: "CountryAlbum",
                image: MetasImage.asset("assets/images/country.jpg"), //can be MetasImage.network
              ),
       ); 
    

    and pass it to assetsAudioPlayer.open(audio, showNotification: true);