Flutter- How to know AudioService is stopped?


I am working with audio_service flutter package. I want to pop a player page if Audio Service stops. How to get the Audio Service stopped event? I didn’t find any events to check if service is stopped


(Answer update: Since v0.18, the service is effectively always running while the app is running, so there is no longer a need to check. The following answer is for v0.17 and earlier.)

AudioService.running will emit true when the service is running and false when it is not.

To listen to when it changes from true to false, you could try this:

// Cast runningStream from dynamic to the correct type.
final runningStream =
    AudioService.runningStream as ValueStream<bool>;
// Listen to stream pairwise and observe when it becomes false
runningStream.pairwise().listen((pair) {
  final wasRunning = pair.first;
  final isRunning = pair.last;
  if (wasRunning && !isRunning) {
    // take action

If you instead want to listen to the stopped playback state, you need to ensure that your background audio task actually emits that state change in onStop:

  Future<void> onStop() async {
    await _player.dispose();
    // the "await" is important
    await AudioServiceBackground.setState(
        processingState: AudioProcessingState.stopped);
    // Shut down this task
    await super.onStop();

This way, you can listen for this state in the UI:

AudioService.playbackStateStream.listen((state) {
  if (state.processingState == AudioProcessingState.stopped)) {
    // take action

Answered By – Ryan Heise

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *