Flutter how to swipe audio_service package's notification?

686

As a useful tip, the easiest way to search the entire documentation of audio_service to find an answer to some question is to navigate into the audio_service.dart file (which contains the whole plugin), and then search for the word "swipe". You will find this option on AudioServiceConfig:

  /// Whether the Android service should switch to a lower priority state when
  /// playback is paused allowing the user to swipe away the notification. Note
  /// that while in this lower priority state, the operating system will also be
  /// able to kill your service at any time to reclaim resources.
  final bool androidStopForegroundOnPause;

And then inside the AudioHandler:

  /// Handle the notification being swiped away (Android).
  Future<void> onNotificationDeleted();

(You may find this easier to search than reading the online HTML documentation.)

What the above means is that you need to pass androidStopForegroundOnPause: true as a parameter to AudioServiceConfig and then if you want to handle any special behaviour other than the default (stop the service) when the user swipes away the notification, you can optionally override onNotificationDeleted in your own audio handler. There is no equivalent for iOS.

Note that it is not possible to swipe away the Android notification while audio is playing, but the option mentioned above will allow you to swipe away the notification from the paused state.

Share:
686
Dani Raducu
Author by

Dani Raducu

Updated on December 25, 2022

Comments

  • Dani Raducu
    Dani Raducu over 1 year

    Currently, the only way to close the notification is by pressing the stop button. But I wonder how can I wrap up the notification widget with a swipeable "thing". Can you tell me any package that would work on this or code that I have to wrap up a thing from the audio_service package?

    The audio_service package publisher is - ryanheise.com