AudioPlayer A Flutter audio plugin (ObjC/Java) to play remote or local audio files.
To use this plugin :
dependencies:
flutter:
sdk: flutter
audioplayer:
//...
AudioPlayer audioPlugin = new AudioPlayer();
//...
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();
});
}
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.
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>
Author: rxlabz
GitHub: https://github.com/rxlabz/audioplayer
#flutter #dart #programming