Remove items from just_audio playlist

68 Views Asked by At

I am using just_audio with audio_service. When song file from current playlist gets deleted by user, player have to make adjustments to it's loaded queue.

When I just delete specific indices:

Future<void> deleteFromQueue(List<Item> deleted) async {
    List<int> results = [];
    List<String?> paths = deleted.map((e) => e.path).toList();

    List<IndexedAudioSource> sources = _songQueue.sequence;
    for( var i = 0; i < sources.length; i++ ){
      if( paths.contains(sources[i].tag.id) ){
        results.add(i);
      }
    }

    int removed = 0;
    for (int index in results) {
      await removeQueueItemAt(index-removed);
      removed++;
    }
}

@override
Future<void> removeQueueItemAt(int index) async {
    if( _songQueue.length > index ){
        _songQueue.removeAt(index);

        final newQueue = queue.value..removeAt(index);
        queue.add(newQueue);
    }
}

It works, but only when files below currently playing song are deleted, if files are above it still works but wrecks current song duration/position slider...

I've also tried not to alter queue, and make check if file exists on auto advance:

_player.positionDiscontinuityStream.listen((discontinuity) {
  if (discontinuity.reason == PositionDiscontinuityReason.autoAdvance) {
    ...
    // obtained next valid index
    if( nextIndex != null ){
      _player.seek(Duration.zero, index: nextIndex);
    } else { _player.stop(); }
  }
});

But getting error with seek method:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: Cannot fire new event. Controller is already firing an event

How can I make this delete thing work?

0

There are 0 best solutions below