Can I hide Android Notification bar if I used Audio Service in Flutter?

731 Views Asked by At

I used the latest Dart package of audio_service (0.18.4). My use case is different. I have main menu on the first screen and another sub-menu to play music in the other screen. I just want to hide the Notification bar after user click on close button to music player. Close here means that user stops playing music and goes directly to main menu.

I started and used get_it package as singleton to run AudioPlayerHandler class in all screens.

import 'package:audio_player_final/audio/audio_handler.dart';
import 'package:get_it/get_it.dart';

GetIt getThem = GetIt.instance;

Future<void> setup() async {
  getThem.registerSingleton<AudioPlayerHandler>(await initAudio(), dispose: (audio) => audio.customAction("dispose"));
}

This is the code if user click close button in Music Player:

  Future<void> customAction(String name, [Map<String, dynamic>? extras]) async {
    if (name == "close") {
      await stop();
      await setShuffleMode(AudioServiceShuffleMode.none);
      await setRepeatMode(AudioServiceRepeatMode.none);
      playbackState.add(playbackState.value.copyWith(
        repeatMode: AudioServiceRepeatMode.none,
        shuffleMode: AudioServiceShuffleMode.none,
      ));

      if (_playlist.children.isNotEmpty) {
        for (int i = _playlist.children.length - 1; i >= 0; i--) {
          await removeQueueItemAt(i);
        }
        dev.log("Cleaning previous playlist");
      }
    }
  }

What I want is to hide Notification bar so user won't press it since there are no more queue of music inside it. I had removed them all in above function if user click close button. If user press the Playback button on Notification bar, the error will get displayed that the queue is empty. I don't want that to happen.

If I use dispose function, the Notification will be disappeared, but audio_service is not meant to restart the init function. So it will be impossible to run the music player again if user want to play music again as it has been disposed.

  Future<void> customAction(String name, [Map<String, dynamic>? extras]) async {
    if (name == "dispose") {
      await stop();
      await _player.dispose();
    } 
  }

Is there anyway to hide the Notification bar of Music Player from audio_service?

Any tips and trick will be welcomed.
Thanks

0

There are 0 best solutions below