Sagar Shende
Sagar Shende

Reputation: 271

How to Clean Audio Playlist in audio service flutter app

How can I clean the Audio Playlist ConcatenatingAudioSource after adding some Audio Chapters to the PLaylist in Audio Service? I am using the AudioPlayerHandlerImpl Singleton class to access all the Audio Service functionality...

so I want to Clean the Audio Playlist when the user Logout from the app I want to hit the function where all the Audio playlists will be cleaned.

I try to clean the playlist using this function

  final _player = AudioPlayer();
  final _playlist = ConcatenatingAudioSource(children: []);

  Future<void> emptyPlaylist() async {
      await _player
          .setAudioSource(_playlist, preload: false)
          .onError((error, stackTrace) {
        _onError(error, stackTrace, stopService: true);
        return null;
      });

but this function is not clearing the playlist i need help :-)

is this Because of this Function below:-

void _listenForDurationChanges() {
    _player.durationStream.listen((duration) {
      var index = _player.currentIndex;
      final newQueue = queue.value;
      if (index == null || newQueue.isEmpty) return;
      if (_player.shuffleModeEnabled) {
        index = _player.shuffleIndices!.indexOf(index);
      }
      final oldMediaItem = newQueue[index];
      final newMediaItem = oldMediaItem.copyWith(duration: duration);
      newQueue[index] = newMediaItem;
      queue.add(newQueue);
      mediaItem.add(newMediaItem);
    });
  }

  void _listenForCurrentAudioIndexChanges() {
    _player.currentIndexStream.listen((index) {
      final playlist = queue.value;
      if (index == null || playlist.isEmpty) return;
      if (_player.shuffleModeEnabled) {
        index = _player.shuffleIndices!.indexOf(index);
      }
      mediaItem.add(playlist[index]);
    });
  }

  void _listenForSequenceStateChanges() {
    _player.sequenceStateStream.listen((SequenceState? sequenceState) {
      final sequence = sequenceState?.effectiveSequence;
      if (sequence == null || sequence.isEmpty) return;
      final items = sequence.map((source) => source.tag as MediaItem);
      if (items.isEmpty) return;
      queue.add(items.toList());
    });
  }

Upvotes: 0

Views: 53

Answers (0)

Related Questions