A Flutter plugin for Android and iOS with native support for audio playlists, background support, and lock screen controls.
First of all, I'd like to thank codinronan for the exceptional work on the original plugin. It made my work with my apps a lot easier.
This plugin is an adaptation of cordova-plugin-playlist to a Flutter plugin. The native source code and the documentation was based on it.
This plugin intentionally does not display track cover art on the lock screen controls on iOS. Usage of the media image object on iOS is known to cause memory leaks. See the Todo section. The Swift version of that object does not (seem to) contain this memory leak, and rewriting this plugin to use Swift 4 is on the Todo list. This is fully supported on Android, however.
This plugin is not designed to play mixable, rapid-fire, low-latency audio, as you would use in a game. A more appropriate cordova plugin for that use case is cordova-plugin-nativeaudio
Cannot mix audio; again the NativeAudio plugin is probably more appropriate. This is due to supporting the lock screen and command center controls: only an app in command of audio can do this, otherwise the controls have no meaning. I would like to add an option to do this, it should be fairly straightforward; at the cost of not supporting the OS-level controls for that invokation.
If you are running this on iOS 9.3, this plugin requires a promise polyfill for the JS layer.
Add flutter_plugin_playlist
as a dependency in pubspec.yml
For help on adding as a dependency, view the documentation.
Android
Android normally will give you ~2-3 minutes of background playback before killing your audio. Adding the WAKE_LOCK permission allows the plugin to utilize additional permissions to continue playing.
<uses-permission android:name="android.permission.WAKE_LOCK" />
iOS
iOS will immediately stop playback when the app goes into the background if you do not include the audio
UIBackgroundMode
. iOS has an additional requirement that audio playback must never stop; when it does, the audio session will be terminated and playback cannot continue without user interaction.
Import the library with:
import 'package:flutter_plugin_playlist/flutter_plugin_playlist.dart';
Use the class RmxAudioPlayer
for interacting directly with the plugin.
RmxAudioPlayer player = new RmxAudioPlayer();
player.initialize();
player.on('status', (eventName, {dynamic args}) {
// TODO handle the status update
});
rmxAudioPlayer.setPlaylistItems(
[
new AudioTrack(
album: "Friends",
artist: "Bon Jovi",
assetUrl: "https://www.soundboard.com/mediafiles/22/223554-d1826dea-bfc3-477b-a316-20ded5e63e08.mp3",
title: "I'll be there for you"
),
new AudioTrack(
album: "Friends",
artist: "Ross",
assetUrl: "assets://assets/223554-fea5dfff-6c80-4e13-b0cf-9926198f50f3.mp3",
title: "The Sound"
),
new AudioTrack(
album: "Friends",
artist: "Friends",
assetUrl: "https://www.soundboard.com/mediafiles/22/223554-3943c7cb-46e0-48b1-a954-057b71140e49.mp3",
title: "F.R.I.E.N.D.S"
),
],
options: new PlaylistItemOptions(
startPaused: true,
)
);
rmxAudioPlayer.play();
These are the available resources:
Make sure to check the example and also the original cordova plugin for more details.
For Android, you can replace the notification icon by adding it at res/mipmap/icon.png.
A fast adaptation was the easier solution to my specific needs, so there is lots to improve in the dart code, like creating flutter widgets to encapsulate the service manipulation.
Any evolutions are welcome.
Plus, what was mapped by cordova-plugin-playlist:
There's so much more to do on this plugin. Some items I would like to see added if anyone wants to help:
cordova-plugin-media
AudioPlayer
includes some smart network recovery featuresAs already mentioned, this plugin is an adaptation of cordova-plugin-playlist:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.