assets_audio_player

Play music/audio stored in assets files directly from Flutter.

No needed to copy songs to a media cache, with assets_audio_player you can open them directly from the assets.

  1. Create an audio directory in your assets (not necessary named "audios")
  2. Declare it inside your pubspec.yaml
flutter:
  assets:
    - assets/audios/

Getting Started

AssetsAudioPlayer.open(AssetsAudio(
    asset: "song1.mp3",
    folder: "assets/audios/",
));
AssetsAudioPlayer.playOrPause();
AssetsAudioPlayer.play();
AssetsAudioPlayer.pause();
AssetsAudioPlayer.seek(Duration to);
AssetsAudioPlayer.stop();

Listeners

All listeners exposes Streams Using RxDart, AssetsAudioPlayer exposes some listeners as ValueObservable (Observable that provides synchronous access to the last emitted item);

Current song

//The current playing audio, filled with the total song duration
AssetsAudioPlayer.current //ValueObservable<PlayingAudio>

//Retrieve directly the current played asset
final PlayingAudio playing = AssetsAudioPlayer.current.value;

//Listen to the current playing song
AssetsAudioPlayer.current.listen((playingAudio){
    final asset = playingAudio.assetAudio;
    final songDuration = playingAudio.duration;
})

Current position (in seconds)

AssetsAudioPlayer.currentPosition //ValueObservable<Duration>

//retrieve directly the current song position
final Duration position = AssetsAudioPlayer.currentPosition.value;

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

IsPlaying

boolean observable representing the current mediaplayer playing state

AssetsAudioPlayer.isPlaying // ValueObservable<bool>

//retrieve directly the current player state
final bool playing = AssetsAudioPlayer.isPlaying.value;

//will follow the AssetsAudioPlayer playing state
return StreamBuilder(
    stream: AssetsAudioPlayer.isPlaying,
    builder: (context, asyncSnapshot) {
        final bool isPlaying = asyncSnapshot.data;
        return Text(isPlaying ? "Pause" : "Play");  
    }),

Finished

Called when the current song has finished to play

AssetsAudioPlayer.finished //ValueObservable<bool>

AssetsAudioPlayer.finished.listen((finished){
    
})

Flutter

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Libraries

assets_audio_player