flutter audio player
a flutter audio player plugin (objc/java) to play remote or local audio files.
features
- [x] android & ios
- [x] play (remote file)
- [x] stop
- [x] pause
- [x] oncomplete
- [x] onduration / oncurrentposition
- [x] seek
- [x] mute
usage
to use this plugin :
- add the dependency to your pubspec.yaml file.
dependencies:
flutter:
sdk: flutter
audioplayer:
- instantiate an audioplayer instance
//...
audioplayer audioplugin = new audioplayer();
//...
audio player controls
future<void> play() async {
await audioplayer.play(kurl);
setstate(() => playerstate = playerstate.playing);
}
future<void> pause() async {
await audioplayer.pause();
setstate(() => playerstate = playerstate.paused);
}
future<void> stop() async {
await audioplayer.stop();
setstate(() {
playerstate = playerstate.stopped;
position = new duration();
});
}
status and current position
the dart part of the plugin listen for platform calls :
//...
_positionsubscription = audioplayer.onaudiopositionchanged.listen(
(p) => setstate(() => position = p)
);
_audioplayerstatesubscription = audioplayer.onplayerstatechanged.listen((s) {
if (s == audioplayerstate.playing) {
setstate(() => duration = audioplayer.duration);
} else if (s == audioplayerstate.stopped) {
oncomplete();
setstate(() {
position = duration;
});
}
}, onerror: (msg) {
setstate(() {
playerstate = playerstate.stopped;
duration = new duration(seconds: 0);
position = new duration(seconds: 0);
});
});
do not forget to cancel all the subscriptions when the widget is disposed.
ios
ios app transport security
by default ios forbids loading from non-https url. to cancel this restriction edit your .plist and add :
<key>nsapptransportsecurity</key>
<dict>
<key>nsallowsarbitraryloads</key>
<true/>
</dict>
Comments are closed.