init<T extends AudioHandler> static method

Future<T> init<T extends AudioHandler>({
  1. required T builder(),
  2. AudioServiceConfig? config,
  3. BaseCacheManager? cacheManager,
})

Register the app's AudioHandler with configuration options. This must be called once during the app's initialisation so that it is prepared to handle audio requests immediately after a cold restart (e.g. if the user clicks on the play button in the media notification while your app is not running and your app needs to be woken up).

You may optionally specify a cacheManager to use when loading artwork to display in the media notification and lock screen. This defaults to DefaultCacheManager.

This may throw a PlatformException on Android if you have not set the correct Service or Activity in your AndroidManifest.xml file or if your Activity does not provide the correct FlutterEngine.

Implementation

static Future<T> init<T extends AudioHandler>({
  required T Function() builder,
  AudioServiceConfig? config,
  BaseCacheManager? cacheManager,
}) async {
  assert(_cacheManager == null);
  config ??= const AudioServiceConfig();
  assert(config.fastForwardInterval > Duration.zero);
  assert(config.rewindInterval > Duration.zero);
  WidgetsFlutterBinding.ensureInitialized();
  _cacheManager = (cacheManager ??= DefaultCacheManager());
  final callbacks = _HandlerCallbacks();
  _platform.setHandlerCallbacks(callbacks);
  await _platform.configure(ConfigureRequest(config: config._toMessage()));
  _config = config;
  final handler = builder();
  _handler = handler;
  callbacks.setHandler(handler);

  _observeMediaItem();
  _observeAndroidPlaybackInfo();
  _observeQueue();
  _observePlaybackState();

  return handler;
}