Symply manage audio session, now playing media and remote commands
import 'package:flutter_media_session/flutter_media_session.dart'
// when initializing your app, set audio session type
await MediaSession.setSession(.music);
// when playing item
await MediaSession.setCommands([.play, .pause]);
final media = MediaItem(title: 'Simplicity', artist: 'Joe Simple', duration: 180, playing: true);
await MediaSession.setMedia(media);
final subscribtion = MediaSession.listen((MediaNotification notif) {
switch(notif.command) {
case .play:
// ...
break;
case .pause:
// ...
break;
default:
break;
}
});
// update item
await MediaSession.setMedia(media.copyWith(position:5));duration and position are expressed in seconds
Join an artUri URI to your MediaItem description
// local file
await MediaSession.setMedia(MediaItem(title: 'my art', artUri: 'file://my/local/file/image.jpg'));
// you can use Uri.file(path).toString() to get a well formated Uri string
// web url
await MediaSession.setMedia(MediaItem(title: 'my art', artUri: 'https://my.web.site/image.jpg')); final subscribtion = MediaSession.listen((MediaNotification notif) {
switch(notif.command) {
case .position:
final double positionInSeconds = notif.value;
// ...
break;
case .repeat:
final MediaRepeatType repeatType = notif.value;
// ...
break;
default:
break;
}
});
Declare the following permissions and service in the manifest
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<service
android:name=".FlutterMediaSessionService"
android:foregroundServiceType="mediaPlayback"
android:exported="true">
<intent-filter>
<action android:name="androidx.media3.session.MediaSessionService"/>
<action android:name="android.media.browse.MediaBrowserService"/>
</intent-filter>
</service>If you like this plugin, you can pay me a coffee.
Contact: renan@aestesis.org
(AI generated image)
