Home > Net >  Flutter - Customly Sort the Current Playing Playlist ( just_audio)
Flutter - Customly Sort the Current Playing Playlist ( just_audio)

Time:05-10

I have been trying to find a way to sort the current playing playlist by every metadata extracted with flutter_media_metadata but so far with no luck.

Is there a way to do it using just_audio?

Edit: here's how the media data is entered:

1: Creating the playlist

2: Set the playlist to the player

CodePudding user response:

This is probably something you need to add yourself. You can take the list of songs in the playlist and write functions to sort them based on each property.

For example:

List<Songs> sortByTitle(List<Songs> playlist, bool isDescending = True) {
  if(isDescending) {
    playlist = playlist.sort((b, a) => a.compareTo(b));
  } else {
    playlist = playlist.sort((a, b) => a.compareTo(b));
  }
  return playlist;
}

Where the list you pass in is a list of titles or whatever metadata you are trying to sort by.

See https://api.dart.dev/stable/2.17.0/dart-core/List/sort.html for more information on the sort method.

CodePudding user response:

I was able to solve the problem by adding a sort function in this abstract class (ShuffleOrder) and then implementing it here (DefaultShuffleOrder). I also added a sort function in this absctract class (AudioSource) and I implemented it in this class (ConcatenatingAudioSource) and finally in the AudioPlayer class I added this sort function:

Future<void> sort(List<Song> songs, PlaylistSorting playlistSorting, {SortingOrder sortingOrder = SortingOrder.ascending}) async {
    if (_disposed) return;
    if (audioSource == null) return;
    _audioSource!._sort(songs, playlistSorting, sortingOrder: sortingOrder);
    _updateShuffleIndices();
    await (await _platform).setShuffleOrder(SetShuffleOrderRequest(audioSourceMessage: _audioSource!._toMessage()));
}
  • Related