just_audio_background 0.0.1-beta.15 copy "just_audio_background: ^0.0.1-beta.15" to clipboard
just_audio_background: ^0.0.1-beta.15 copied to clipboard

An add-on for just_audio that supports background playback and media notifications.

just_audio_background #

This package plugs into just_audio to add background playback support and remote controls (notification, lock screen, headset buttons, smart watches, Android Auto and CarPlay). It supports the simple use case where an app has a single AudioPlayer instance.

If your app has more complex requirements, it is recommended that you instead use the audio_service package directly (which just_audio_background is internally built on). This will give you greater control over which buttons to display in the notification and how you want them to behave, while also allowing you to use multiple audio player instances.

Setup #

Add the just_audio_background dependency to your pubspec.yaml alongside just_audio:

dependencies:
  just_audio: any # substitute version number
  just_audio_background: any # substitute version number

copied to clipboard

Then add the following initialization code to your app's main method (refer to the API documentation for the complete set of options):

Future<void> main() async {
  await JustAudioBackground.init(
    androidNotificationChannelId: 'com.ryanheise.bg_demo.channel.audio',
    androidNotificationChannelName: 'Audio playback',
    androidNotificationOngoing: true,
  );
  runApp(MyApp());
}
copied to clipboard

Create your player as normal:

player = AudioPlayer();
copied to clipboard

Set a MediaItem tag on each IndexedAudioSource loaded into the player. For example:

AudioSource.uri(
  Uri.parse('https://example.com/song1.mp3'),
  tag: MediaItem(
    // Specify a unique ID for each media item:
    id: '1',
    // Metadata to display in the notification:
    album: "Album name",
    title: "Song name",
    artUri: Uri.parse('https://example.com/albumart.jpg'),
  ),
),
copied to clipboard

Android setup #

Make the following changes to your project's AndroidManifest.xml file:

<manifest xmlns:tools="http://schemas.android.com/tools" ...>
  <!-- ADD THESE TWO PERMISSIONS -->
  <uses-permission android:name="android.permission.WAKE_LOCK"/>
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
  <!-- ALSO ADD THIS PERMISSION IF TARGETING SDK 34 -->
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"/>
  
  <application ...>
    
    ...
    
    <!-- EDIT THE android:name ATTRIBUTE IN YOUR EXISTING "ACTIVITY" ELEMENT -->
    <activity android:name="com.ryanheise.audioservice.AudioServiceActivity" ...>
      ...
    </activity>
    
    <!-- ADD THIS "SERVICE" element -->
    <service android:name="com.ryanheise.audioservice.AudioService"
        android:foregroundServiceType="mediaPlayback"
        android:exported="true" tools:ignore="Instantiatable">
      <intent-filter>
        <action android:name="android.media.browse.MediaBrowserService" />
      </intent-filter>
    </service>

    <!-- ADD THIS "RECEIVER" element -->
    <receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver"
        android:exported="true" tools:ignore="Instantiatable">
      <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
      </intent-filter>
    </receiver> 
  </application>
</manifest>
copied to clipboard

Note: when targeting Android 12 or above, you must set android:exported on each component that has an intent filter (the main activity, the service and the receiver). If the manifest merging process causes "Instantiable" lint warnings, use tools:ignore="Instantiable" (as above) to suppress them.

If your app has a requirement to use a FragmentActivity, you can replace AudioServiceActivity above with AudioServiceFragmentActivity. If your app needs to use a custom activity, you can also make your own activity class a subclass of either AudioServiceActivity or AudioServiceFragmentActivity. For more details on this and other options, refer to the audio_service setup instructions.

iOS setup #

Insert this in your Info.plist file:

	<key>UIBackgroundModes</key>
	<array>
		<string>audio</string>
	</array>
copied to clipboard
335
likes
160
points
21.3k
downloads

Publisher

verified publisherryanheise.com

Weekly Downloads

2024.09.10 - 2025.03.25

An add-on for just_audio that supports background playback and media notifications.

Repository (GitHub)
View/report issues
Contributing

Topics

#audio #sound #player #background

Documentation

API reference

License

MIT (license)

Dependencies

audio_service, audio_session, flutter, flutter_web_plugins, just_audio_platform_interface, meta, rxdart

More

Packages that depend on just_audio_background