tungvt01 / Flutter-AssetsAudioPlayer

Play music/audio stored in assets files directly from Flutter

Home Page:https://pub.dartlang.org/packages/assets_audio_player

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

assets_audio_player

pub package

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

final assetsAudioPlayer = AssetsAudioPlayer();

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.

About

Play music/audio stored in assets files directly from Flutter

https://pub.dartlang.org/packages/assets_audio_player

License:Apache License 2.0


Languages

Language:Dart 49.7%Language:Swift 19.9%Language:Kotlin 19.9%Language:Ruby 9.0%Language:Objective-C 1.3%Language:Shell 0.2%