This is a fork of Juan Potter's audioplayers, with the difference that it plays the audio in the background without muting other audio playing in other apps.
It has the exact same API.
Just import the fork, which is named audioplayersbg
(mind the 'sbg'), instead of the original:
dependencies:
...
audioplayersbg: ^0.5.3
Also, in 0.2.0
, I've added the ability to disable logs with:
AudioPlayer.logEnabled = false;
A Flutter audio plugin.
-
Android & iOS
- play (remote and local file)
- stop
- pause
- seek
- onComplete
- onDuration / onCurrentPosition
-
Supported formats
To use this plugin :
- add the dependency to your pubspec.yaml file.
dependencies:
flutter:
sdk: flutter
audioplayersbg:
- instantiate an AudioPlayer instance
//...
AudioPlayer audioPlayer = new AudioPlayer();
//...
play() async {
final result = await audioPlayer.play(kUrl);
if (result == 1) setState(() => playerState = PlayerState.playing);
}
// add a isLocal parameter to play a local file
playLocal() async {
final result = await audioPlayer.play(kUrl, isLocal: true);
if (result == 1) setState(() => playerState = PlayerState.playing);
}
pause() async {
final result = await audioPlayer.pause();
if (result == 1) setState(() => playerState = PlayerState.paused);
}
stop() async {
final result = await audioPlayer.stop();
if (result == 1) setState(() => playerState = PlayerState.stopped);
}
// seek 5 seconds from the beginning
audioPlayer.seek(5.0);
The Dart part of the plugin listen for platform calls :
//...
audioPlayer.setDurationHandler((Duration d) => setState(() {
duration = d;
}));
audioPlayer.setPositionHandler((Duration p) => setState(() {
position = p;
}));
audioPlayer.setCompletionHandler(() {
onComplete();
setState(() {
position = duration;
});
});
audioPlayer.setErrorHandler((msg) {
print('audioPlayer error : $msg');
setState(() {
playerState = PlayerState.stopped;
duration = new Duration(seconds: 0);
position = new Duration(seconds: 0);
});
});
- this plugin is written in swift, so to use with in a Flutter/ObjC project, you need to convert the project to "Current swift syntax" ( Edit/Convert/current swift syntax)
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>
For help getting started with Flutter, view our online documentation.
For help on editing plugin code, view the documentation.