Use third-party plug-ins to achieve audio playback, support Android, IOS GitHub - rxlabz/audioplayer: A flutter plugin to play audio files iOS / Android / MacOS / Web ( Swift/Java ) https://github.com/rxlabz/audioplayer
There is a bug here
audioplayer: 0.8.1 localization will appear not found
MethodChannel('bz.rxla.flutter/audio');
change to this
audioplayer:
path: G:\ren_work\flutter\project name\package name written by myself\audioplayer
Usage
To use this plugin :
- Add the dependency to your pubspec.yaml file.
audioplayer: 0.8.1
- Instantiate an AudioPlayer instance
//... AudioPlayer audioPlugin = AudioPlayer(); //...
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
MacOS
Add this to entitlements files ( cf. DebugProfile.entitlements )
<key>com.apple.security.network.client</key> <true/>