suragch / flutter_audio_service_demo

Companion project for Flutter audio_service tutorial
https://suragch.medium.com/background-audio-in-flutter-with-audio-service-and-just-audio-3cce17b4a7d?sk=0837a1b1773e27a4f879ff3072e90305
MIT License
57 stars 28 forks source link

How to pass an Item to the MyAudioHandler #12

Closed zionnite closed 1 year ago

zionnite commented 2 years ago

Hello sir, trust your day is going well?

pls I have a feature am working on, I list all the audio music in a ListView, and on Item, click pass that item into the MyAudioHandler inside the

ConcatenatingAudioSource(
      children: [
        ...ITEM CLICK HERE
      ],
    );
zionnite commented 2 years ago

Hello sir, After much studying your good works I discovered I was doing it wrong, what I needed to do was to pass the params to the controller and from there I was able to pass it to the page_manager and then, I was able to add it to the queue, doing it this way it works very well but the issue I have is that, the moment i comment out _loadPlaylist() and hotreload, the player will continue to show loading indicator but the Song name is always show but the music will continue to load

message_player Controller i called the below method inside the initState()

addMessageToPlayer({
  required String id,
  required String title,
  required String album,
  required String url,
  required String artUri,
}) {
  final pageManager = getIt<PageManager>();
  pageManager.addMessageToPlayer(
    id: id,
    title: title,
    album: album,
    url: url,
    artUri: artUri,
  );
}

page_manager.dart inside the Init() i commented out the _loadPlaylist() because i don't need it

void init() async {
//    await _loadPlaylist();
    _listenToPlaybackState();
    _listenToCurrentPosition();
    _listenToBufferedPosition();
    _listenToTotalDuration();
    _listenToChangesInSong();
    _listenToChangesInPlaylist();
  }

Here is the function that adds the items from the controller to the queue

  addMessageToPlayer(
      {required String id,
      required String title,
      required String album,
      required String url,
      required String artUri}) {
    // print('ID came ${id}');
    // print('TITLE came ${title}');
    // print('ALBUM came ${album}');
    // print('URL came ${url}');
    // print('ARTURL came ${artUri}');

    final mediaItem = MediaItem(
      id: id,
      title: title,
      album: album,
      extras: {
        'url': url,
        'artUri': artUri,
      },
    );
    _audioHandler.addQueueItem(mediaItem);
  }

Note: If _loadPlaylist() is not commented out, it will work very well

Please what am i missing? Screen Shot 2021-11-03 at 1 14 21 PM Screen Shot 2021-11-03 at 1 15 32 PM

zionnite commented 2 years ago

I was able to solve the issue, what I did was to go inside the addQueueItem block and add player.setAudioSource(_playlist); to set the audio player to the latest item

suragch commented 1 year ago

Sorry for my late reply. I'm glad you were able to solve your issue. Thank you for posting the solution for others with the same problem.