audio_service 0.0.3

  • Example
  • Installing
  • Versions
  • new48


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, headset, Wear OS or Android Auto
  • Drive audio playback from Dart code

This plugin provides a complete framework for playing audio in the background. You implement callbacks in Dart to play/pause/seek/etc audio, which means that you will need to use other Dart plugins in conjunction with this one to actually play the audio. This gives you the flexibility to play any kind of audio in the background. For example, if you want to play music in the background, you may use the audioplayer plugin in conjunction with this one. If you would rather play text-to-speech in the background, you may use the flutter_tts plugin in conjunction with this one.

audio_service itself manages all of the platform-specific code for setting up the environment for background audio, and interfacing with various peripherals used to control audio playback. For Android, this means acquiring a wake lock so that audio will play with the screen turned off, acquiring audio focus so that you can control your app from a headset, creating a media session and media browser service so that your app can be controlled by wearable devices and Android Auto. The iOS side is currently not implemented and so contributors are welcome (please see the Help section at the bottom of this page).

Since background execution of Dart code is a relatively new feature of flutter, not all plugins are yet compatible with audio_service (I am contacting some of these authors to make their packages compatible.)


Client-side code:

  backgroundTask: myBackgroundTask,
  notificationChannelName: 'Music Player',
  androidNotificationIcon: "mipmap/ic_launcher",

Background code:

void myBackgroundTask() {
  Completer completer = Completer();
  MyAudioPlayer player = MyAudioPlayer();
    onStart: () async {;
      // Keep the background environment alive
      // Until we're finished playing...
      await completer.future;
    onStop: () {
    onClick: (MediaButton button) {

class MyAudioPlayer {
  // your custom dart code

Android manifest file:

<manifest ...>
  <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <service android:name="com.ryanheise.audioservice.AudioService">
        <action android:name="" />

    <receiver android:name="" >
        <action android:name="android.intent.action.MEDIA_BUTTON" />

Application class:

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

  public void registerWith(PluginRegistry registry) {


  • If you know how to implement any of these features in iOS, pull requests are welcome! As a guideline, prefer to keep the same dart API for both Android and iOS where possible. In cases where there are unavoidable differences between Android and iOS, name the feature with an android or ios prefix.

  • If you find another flutter plugin (audio or otherwise) that crashes when running in the background environment, another way you can help is to file a bug report with that project, letting them know of the simple fix to make it work (see below).

Sample bug report

Here is a sample bug report.

Flutter's new background execution feature (described here: allows plugins to be registered in a background context (e.g. a Service). The problem is that the wifi plugin assumes that the context for plugin registration is an activity with this line of code:

WifiManager wifiManager = (WifiManager) registrar.activity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);

registrar.activity() may now return null, and this leads to a NullPointerException:

E/AndroidRuntime( 2453):   at
E/AndroidRuntime( 2453):   at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(

The solution is to change the above line of code to this:

WifiManager wifiManager = (WifiManager) registrar.activeContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);


  • Pause now keeps background isolate running
  • Notification channel id is generated from package name
  • Updated example to use audioplayer plugin
  • Fixed media button handling


  • Better connection handling.


  • Initial release.



Demonstrates how to use the audio_service plugin.

Getting Started

For help getting started with Flutter, view our online documentation.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  audio_service: ^0.0.3

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:audio_service/audio_service.dart';
Version Uploaded Documentation Archive
0.0.3 Dec 7, 2018 Go to the documentation of audio_service 0.0.3 Download audio_service 0.0.3 archive
0.0.2 Nov 29, 2018 Go to the documentation of audio_service 0.0.2 Download audio_service 0.0.2 archive
0.0.1 Nov 28, 2018 Go to the documentation of audio_service 0.0.1 Download audio_service 0.0.1 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Dec 7, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7
  • Flutter: 1.0.0


Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While there is nothing inherently wrong with versions of 0.0.*, it usually means that the author is still experimenting with the general direction of the API.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8