Today we are introduce another flutter plugin for accessing sound files from local storage and playing music on android and ios devices.@audioplayer This will be helpful for all developers who intend to create an audio application or audio player.
Using this plugins we can play music from both remote and local file (playing audio from local device is not supported in Web version).Just like other audio player we can handle Stop,pause,seek ,mute etc functionality.There are 3 major event like oncomplete,onDuration,OnCurrentpositions are very helpful for manage overall functionality of application like autoplayback ,display total and current duration in application etc.
This Library is totally opensource and you can download it from Github server.
Usage
To use this plugin :
- Add following dependency to your pubspec.yaml file.
dependencies:
flutter:
sdk: flutter
audioplayer: 0.8.1
audioplayer_web: 0.7.1
- Instantiate an AudioPlayer instance
AudioPlayer audioPlugin = AudioPlayer();
Audio Player Controls
audioPlayer.play(url);
audioPlayer.pause();
audioPlayer.stop();
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>
Background mode
Mac OS
Add this to entitlements files ( cf. DebugProfile.entitlements )
<key>com.apple.security.network.client</key> <true/>