Not getting headset play/pause event using audio_service

267 Views Asked by At

I have a Flutter app running on iOS and I want to use the play/pause button on a wired headset to serve as a way to remotely trigger an action in my app. It's not a music app, so my implementation is simple. I'm using audio_service 0.18.4 and I think I've done it right, but I get no events at all. I've tried using a headset with a lightening connector and a 3.5mm jack with a lightening adapter. A swift app can get these events BTW, so the headsets are good.

I have added audio to UIBackgroundModes in the info.plist file:

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

I'm instantiating the button handler:

  final mediaButtonConfig = const AudioServiceConfig(
      androidNotificationChannelId: 'com.REDACTED.channel.buttons',
      androidNotificationChannelName: 'Button events',
      androidNotificationOngoing: true,);

      mediaButtonHandler = await AudioService.init(builder: () => MediaButtonHandler(), config: mediaButtonConfig);

The button handler itself:

import 'package:audio_service/audio_service.dart';

class MediaButtonHandler extends BaseAudioHandler {

  MediaButtonHandler() {
    // Nothing really to play here. This is just to intercept media play/pause button to do my thing...
    _log.d('Initializing media buttons');
  }

  // I'm looking for a play/pause event.
  @override
  Future<void> click([MediaButton button = MediaButton.media]) async {
    _log.d('MediaButton button pressed');
    // Do my thing...
  }

  // I don't need this, but here for testing..
  @override
  Future<void> play() async {
    _log.d('Play button pressed');
  }

  // I don't need this, but here for testing..
  @override
  Future<void> pause() async {
    _log.d('Pause button pressed');
  }

  // I don't need this, but here for testing..
  @override
  Future<void> skipToNext() async {
    _log.d('Skip to next button pressed');
  }

  // I don't need this, but here for testing..
  @override
  Future<void> skipToPrevious() async {
    _log.d('Skip to previous button pressed');
  }

  // I don't need this, but here for testing..
  @override
  Future<void> stop() async {
    _log.d('Stop button pressed');
  }
}

I can see the log entry for the button handler constructor, so it should be working. I'm hoping I've missed something. Any ideas?

0

There are 0 best solutions below