Flutter package Audio Service

[Fuente: https://pub.dev/packages/audio_service]

Play audio in the background.

  • Continues playing while the screen is off or the app is in the background
  • Control playback from your Flutter UI, notifications, lock screen, headset, Wear OS or Android Auto
  • Drive audio playback from Dart code

This plugin wraps around your existing Dart audio code to allow it to run in the background, and also respond to media button clicks on the lock screen, notifications, control center, headphone buttons and other supported remote control devices. This is necessary for a whole range of media applications such as music and podcast players, text-to-speech readers, navigators, etc.

This plugin is audio agnostic. It is designed to allow you to use your favourite audio plugins, such as audioplayerflutter_radioflutter_tts, and others. It simply wraps a special isolate around your existing audio code so that it can run in the background and enable remote control interfaces.

Note that because your app’s UI and your background audio task will run in separate isolates, they do not share memory. They communicate through the message passing APIs provided by audio_service.

Example #

audio_service provides two sets of APIs: one for your main UI isolate (AudioService), and one for your background audio isolate (AudioServiceBackground).

UI code #

This code runs in the main UI isolate:

AudioService.connect();    // When UI becomes visible
AudioService.start(        // When user clicks button to start playback
  backgroundTaskEntrypoint: myBackgroundTaskEntrypoint,
  androidNotificationChannelName: 'Music Player',
  androidNotificationIcon: "mipmap/ic_launcher",
AudioService.pause();      // When user clicks button to pause playback
AudioService.play();       // When user clicks button to resume playback
AudioService.disconnect(); // When UI is gone

The full example on GitHub should be consulted for tips on how to hook connect and disconnect into your widget’s lifecycle.

Background code #

This code runs in a background isolate, and is the code that is guaranteed to continue running even if your UI is gone:

void myBackgroundTaskEntrypoint() {
  AudioServiceBackground.run(() => MyBackgroundTask());

class MyBackgroundTask extends BackgroundAudioTask {
  Future<void> onStart() async {
    // Your custom dart code to start audio playback.
    // NOTE: The background audio task will shut down
    // as soon as this async function completes.
  void onStop() {
    // Your custom dart code to stop audio playback.
  void onPlay() {
    // Your custom dart code to resume audio playback.
  void onPause() {
    // Your custom dart code to pause audio playback.
  void onClick(MediaButton button) {
    // Your custom dart code to handle a media button click.

The full example on GitHub demonstrates how to fill in these callbacks to do audio playback and also text-to-speech.

Android setup #

  1. You will need to create a custom MainApplication class as follows:
// Insert your package name here instead of com.example.yourpackagename.
// You can find your package name at the top of your AndroidManifest file
// after package="...
package com.example.yourpackagename;

import io.flutter.plugin.common.PluginRegistry;
import io.flutter.app.FlutterApplication;
import io.flutter.plugins.GeneratedPluginRegistrant;
import com.ryanheise.audioservice.AudioServicePlugin;

public class MainApplication extends FlutterApplication implements PluginRegistry.PluginRegistrantCallback {
  public void onCreate() {

  public void registerWith(PluginRegistry registry) {
  1. Edit your project’s AndroidManifest.xml file to reference your MainApplication class, declare the permission to create a wake lock, and add component entries for the <service> and <receiver>:
<manifest ...>
  <uses-permission android:name="android.permission.WAKE_LOCK"/>
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <service android:name="com.ryanheise.audioservice.AudioService">
        <action android:name="android.media.browse.MediaBrowserService" />

    <receiver android:name="androidx.media.session.MediaButtonReceiver" >
        <action android:name="android.intent.action.MEDIA_BUTTON" />
  1. Any icons that you want to appear in the notification (see the MediaControl class) should be defined as Android resources in android/app/src/main/res. Here you will find a subdirectory for each different resolution:

You can use Android Asset Studio to generate these different subdirectories for any standard material design icon.

NOTE: Most Flutter plugins today were written before Flutter added support for running Dart code in a headless environment (without an Android Activity present). As such, a number of plugins assume there is an activity and run into a NullPointerException. Fortunately, it is very easy for plugin authors to update their plugins remove this assumption. If you encounter such a plugin, see the bottom of this README file for a sample bug report you can send to the relevant plugin author.