Skip to content

wyrindev/flutter-media-session

Repository files navigation

flutter_media_session

A powerful Flutter plugin for integrating your app with system-level media controls (lock screen, notification, media center) across Android, iOS, macOS, Windows, and Web.

This plugin allows your app to display media metadata (title, artist, artwork) in the system's media center and respond to system actions like Play, Pause, Skip, and Seek.

Platform Support

Platform Support Underlying API
Android Media3 MediaSessionService
iOS MPNowPlayingInfoCenter / MPRemoteCommandCenter
macOS MPNowPlayingInfoCenter / MPRemoteCommandCenter
Windows SystemMediaTransportControls (SMTC)
Web Media Session API

Features

  • 🎵 Metadata Synchronization: Display title, artist, album, and artwork in the lock screen or system media center.
  • ⏯️ Playback State Control: Sync playing/paused status and current playback position.
  • 📡 Native Media Actions: Receive events from system controls (Play, Pause, Skip, Seek, etc.) and handle them in your Dart code.
  • 🎧 Android Background Support: Automatically handles foreground service requirements for media playback on Android.

Installation

Add flutter_media_session to your pubspec.yaml:

dependencies:
  flutter_media_session: ^2.0.0

Setup

Android

  1. Foreground Service Permission: Ensure your android/app/src/main/AndroidManifest.xml includes the necessary permissions (usually added by the plugin automatically, but worth verifying):

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
  2. Service Declaration: The plugin handles the service declaration internally via its manifest merger.

iOS

  1. Background Audio: Add the audio background mode to your Info.plist:

    <key>UIBackgroundModes</key>
    <array>
        <string>audio</string>
    </array>

    This allows the Now Playing controls to work when the app is backgrounded.

  2. Audio Session: Call requestNotificationPermission() before activating the session. On iOS, this configures the AVAudioSession with the .playback category.

macOS

No specific configuration is required. The plugin uses MPNowPlayingInfoCenter and MPRemoteCommandCenter directly.

Windows

For proper application identification (avoiding "Unknown Application" in the system media center), please refer to the:

Windows Setup Guide

Web

No specific configuration is required. The plugin uses standard JS interop on Web.

Usage

For detailed instructions and examples on how to initialize the plugin, manage media metadata, respond to system media events, and customize available media controls layout dynamically, please refer to our full breakdown:

Detailed Usage Guide

License

This project is licensed under the MIT License - see the LICENSE file for details.