Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

assets_audio_player

play music/audio stored in assets files (simultaneously) directly from flutter (android / ios / web).

you can also use play audio files from network using their url

flutter:
  assets:
    - assets/audios/
assetsaudioplayer.newplayer().open(
    audio("assets/audios/song1.mp3"),
    autoplay: true,
);

assets
sample1

�� import

dependencies:
  assets_audio_player: ^1.4.7+4

works with flutter: ">=1.12.13+hotfix.6 <2.0.0", be sure to upgrade your sdk

�� web support

and if you wan web support, enable web then add

dependencies:
  assets_audio_player_web: ^1.4.7+4

you like the package ? buy me a kofi 🙂

buy me a coffee at ko-fi.com

audio source android ios web
asset file (asset path)
network file (url)
local file (path)
network livestream (url)
commands android ios web
play
pause
stop
seek(position)
seekby(position)
forward(speed)
rewind(speed)
next
prev
feature android ios web
multiple players
open playlist
system notification ��
respect system silent mode ��
pause on phone call ��
audio widget
properties android ios web
loop
get/set volume
get/set play speed
listeners android ios web
listener onready(completeduration)
listener currentposition
listener finished
listener buffering
listener volume
listener play speed

�� import assets files

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(
    audio("assets/audios/song1.mp3"),
);

you can also play network songs from url

final assetsaudioplayer = assetsaudioplayer();

try {
    await assetsaudioplayer.open(
        audio.network("http://www.mysite.com/mymp3file.mp3"),
    );
} catch (t) {
    //mp3 unreachable
}

and play songs from file

//create a new player
final assetsaudioplayer = assetsaudioplayer();

assetsaudioplayer.open(
    audio.file(file_uri),
);
assetsaudioplayer.playorpause();
assetsaudioplayer.play();
assetsaudioplayer.pause();
assetsaudioplayer.seek(duration to);
assetsaudioplayer.seekby(duration by);
assetsaudioplayer.forwardrewind(double speed);
//if positive, forward, if negative, rewind
assetsaudioplayer.stop();

notifications

notification_android

notification_ios

on ios, it will use mpnowplayinginfocenter

  1. add metas inside your audio
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
          ),
   );
  1. open with shownotification: true
_player.open(audio, shownotification: true)

⛓ play in parallel / simultaneously

you can create new assetsaudioplayer using assetsaudioplayer.newplayer(),
which will play songs in a different native media player

this will enable to play two songs simultaneously

you can have as many player as you want !

///play 3 songs in parallel
assetsaudioplayer.newplayer().open(
    audio("assets/audios/song1.mp3")
);
assetsaudioplayer.newplayer().open(
    audio("assets/audios/song2.mp3")
);

//another way, with create, open, play & dispose the player on finish
assetsaudioplayer.playandforget(
    audio("assets/audios/song3.mp3")
);

each player has an unique generated id, you can retrieve or create them manually using

final player = assetsaudioplayer.withid(id: "my_unique_id");

playlist

assetsaudioplayer.open(
  playlist(
    assetaudiopaths: [
      "assets/audios/song1.mp3",
      "assets/audios/song2.mp3"
    ]
  )
);

assetsaudioplayer.next();
assetsaudioplayer.prev();
assetsaudioplayer.playatindex(1);

audio widget

if you want a more flutter way to play audio, try the audiowidget !

audio_widget

//inside a stateful widget

bool _play = false;

@override
widget build(buildcontext context) {
  return audio.assets(
     path: "assets/audios/country.mp3",
     play: _play,
     child: raisedbutton(
           child: text(
               _play ? "pause" : "play",
           ),
           onpressed: () {
               setstate(() {
                 _play = !_play;
               });
           }
      ),
      onreadytoplay: (duration) {
          //onreadytoplay
      },
      onpositionchanged: (current, duration) {
          //onreadytoplay
      },
  );
}

how to �� stop �� the audiowidget ?

just remove the audio from the tree !
or simply keep play: false

�� 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 song duration

//listen to the current playing song
final duration = assetsaudioplayer.current.value.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());  
    }),

or use a playerbuilder !

playerbuilder.currentposition(
     player: _assetsaudioplayer,
     builder: (context, duration) {
       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");  
    }),

or use a playerbuilder !

playerbuilder.isplaying(
     player: _assetsaudioplayer,
     builder: (context, isplaying) {
       return text(isplaying ? "pause" : "play");  
     }
)

�� volume

change the volume (between 0.0 & 1.0)

assetsaudioplayer.setvolume(0.5);

the media player can follow the system “volume mode” (vibrate, muted, normal)
simply set the respectsilentmode optional parameter as true

_player.open(playable, respectsilentmode: true);

https://developer.android.com/reference/android/media/audiomanager.html?hl=fr#getringermode()

https://developer.apple.com/documentation/avfoundation/avaudiosessioncategorysoloambient

listen the volume

return streambuilder(
    stream: assetsaudioplayer.volume,
    builder: (context, asyncsnapshot) {
        final double volume = asyncsnapshot.data;
        return text("volume : $volume");  
    }),

or use a playerbuilder !

playerbuilder.volume(
     player: _assetsaudioplayer,
     builder: (context, volume) {
       return text("volume : $volume");
     }
)

✋ finished

called when the current song has finished to play,

it gives the playing audio that just finished

assetsaudioplayer.playlistaudiofinished //valueobservable<playing>

assetsaudioplayer.playlistaudiofinished.listen((playing playing){
    
})

called when the complete playlist has finished to play

assetsaudioplayer.playlistfinished //valueobservable<bool>

assetsaudioplayer.playlistfinished.listen((finished){
    
})

�� looping

final bool islooping = assetsaudioplayer.loop; //true / false

assetsaudioplayer.loop = true; //set loop as true

assetsaudioplayer.islooping.listen((loop){
    //listen to loop
})

assetsaudioplayer.toggleloop(); //toggle the value of looping

network policies (android/ios)

android only allow https calls, you will have an error if you’re using http,
don’t forget to add internet permission and seet usescleartexttraffic="true" in your androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.internet" />
    <application
        ...
        android:usescleartexttraffic="true"
        ...>
        ...
    </application>
</manifest>

ios only allow https calls, you will have an error if you’re using http,
don’t forget to edit your info.plist and set nsapptransportsecurity to nsallowsarbitraryloads

<key>nsapptransportsecurity</key>
<dict>
    <key>nsallowsarbitraryloads</key>
    <true/>
</dict>

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.