Audio_service: i found no way to add queue from the request data.

Created on 4 Dec 2019  路  11Comments  路  Source: ryanheise/audio_service

my problem
Through the example document, I can't find a way to add a play queue asynchronously. Could u enrich the following example to add a way to add a play queue asynchronously by request. Grateful.

Most helpful comment

Thank you for responding @ryanheise

I have implemented the onAddQueueItem callback as you have explained.

 @override
  void onAddQueueItem(MediaItem mediaItem) {
    _queue.add(mediaItem);
    AudioServiceBackground.setQueue(_queue);
  }

And as you have designed, the MediaItem is added on to the _queue list and is reflected in the audio playing queue.

Thanks a lot, and your plugin is a literal life saver for many of us here and the implementation is so intutive, cannot thank you enough @ryanheise
Hope to contribute to this project at least in documentation or tutorials as soon as I become well versed with the APIs provided.

All 11 comments

@lalilajiao Do you mean "add queue" or "add queue item"?

If you mean you want to add a queue item, then:

  1. Call AudioService.addQueueItem(item) from the UI.
  2. Override the onAddQueueItem callback in the background task to handle that in whatever way you like, typically by adding it to a list data structure where you keep track of the queue, or maybe a database if you want to persist it.
  3. Within your onAddQueueItem callback, once you've actually done(2) above, then call AudioServiceBackground.setQueue to notify all clients that the queue has changed.
  4. In your UI, listen to queueStream so that your UI can reflect changes to the queue, e.g. by displaying it.

I don't think the asynchronous factor should make any difference to the above procedure.

@ryanheise

MediaItem mediaItem = MediaItem(
        id: "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3",
        album: "Science Friday",
        title: "A Salute To Head-Scratching Science",
        artist: "Science Friday and WNYC Studios",
        duration: 5739820,
        artUri:
        "https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg",
 );
AudioService.addQueueItem(mediaItem);
AudioService.start(
        backgroundTaskEntrypoint: entrypoint,
        resumeOnClick: true,
        androidNotificationChannelName: 'Audio Service Demo',
        notificationColor: 0xFF2196f3,
        androidNotificationIcon: 'mipmap/ic_launcher',
);

i did this before call AudioService.start then i got this below.

E/flutter (13011): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, This session doesn't support queue management operations, null)
E/flutter (13011): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (13011): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
E/flutter (13011): <asynchronous suspension>
E/flutter (13011): #2      AudioService.addQueueItem (package:audio_service/audio_service.dart:526:20)
E/flutter (13011): <asynchronous suspension>
E/flutter (13011): #3      _MyAppState.startButton.<anonymous closure> (package:audio_service_example/main.dart:177:20)
E/flutter (13011): #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:654:14)
E/flutter (13011): #5      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:729:32)
E/flutter (13011): #6      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (13011): #7      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
E/flutter (13011): #8      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
E/flutter (13011): #9      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9)
E/flutter (13011): #10     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13)
E/flutter (13011): #11     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11)
E/flutter (13011): #12     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter (13011): #13     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (13011): #14     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (13011): #15     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (13011): #16     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (13011): #17     _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter (13011): #18     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (13011): #19     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (13011): #20     _invoke1 (dart:ui/hooks.dart:263:10)
E/flutter (13011): #21     _dispatchPointerDataPacket (dart:ui/hooks.dart:172:5)

You can't communicate with the background task before you start it, so you must call start before addQueueItem.

You can't communicate with the background task before you start it, so you must call start before addQueueItem.

I called start before addQueueItem , but it still throws the exception
PlatformException(error, This session doesn't support queue management operations, null)

Make sure you await the start call otherwise you may end up still calling addQueueItem before it's actually started.

@guoz2013 Is this issue resolved for you?

Hi, I am facing this same issue and would like to further throw light on the issue...

MediaItem(
      id: "https://s3.amazonaws.com/scifri-segments/scifri201711241.mp3",
      album: "Science Friday",
      title: "From Cat Rheology To Operatic Incompetence",
      artist: "Science Friday and WNYC Studios",
      duration: 2856950,
      artUri:
          "https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg",
);
// start the AudioService
await AudioService.start(
      backgroundTaskEntrypoint: _audioPlayerTaskEntrypoint,
      resumeOnClick: true,
      androidNotificationOngoing: true,
      androidNotificationChannelName: 'Sample Notification Channel',
      notificationColor: 0xFF09090E,
      enableQueue: true,
      androidNotificationIcon: 'mipmap/ic_launcher',
);
AudioService.addQueueItem(currMediaItem);

Even after I await the start of audio service, and then add the MediaItem to the queue, the actual queue is not getting modified. Instead, it remains the same.

I was however able to solve the problem faced by @lalilajiao

Unhandled Exception: PlatformException(error, This session doesn't support queue management operations, null)

which was caused by the missing parameter in AudioService.start()

enableQueue: true

Please advise us on how to solve this problem of unmodifiable AudioService queue

HI @RohitRajP

There is no notion of an "actual queue" in the plugin, only messages to communicate queue operations which you need to implement to maintain your own actual queue. In the same way, there is no "actual audio" in the plugin, only messages to communicate the intent to play audio, which you still need to implement with your own audio playing code.

So another way to investigate what is going wrong is to identify which queue-related message is not being delivered. If you have done something like suggested in my first comment, you'll have implemented the onAddQueueItem callback to update your actual queue and broadcast the updated queue to clients via AudioServiceBackground.setQueue which you should then receive notification of in the client via queueStream. Can you clarify which of these messages is the point of failure? For example, are you not receiving the onAddQueueItem message? Or are you not receiving the queueStream message?

Thank you for responding @ryanheise

I have implemented the onAddQueueItem callback as you have explained.

 @override
  void onAddQueueItem(MediaItem mediaItem) {
    _queue.add(mediaItem);
    AudioServiceBackground.setQueue(_queue);
  }

And as you have designed, the MediaItem is added on to the _queue list and is reflected in the audio playing queue.

Thanks a lot, and your plugin is a literal life saver for many of us here and the implementation is so intutive, cannot thank you enough @ryanheise
Hope to contribute to this project at least in documentation or tutorials as soon as I become well versed with the APIs provided.

Thanks, @RohitRajP

Sorry for the delay in replying! (I assume from your message above that you managed to get your queue behaviour working).

I just want to join and thank you @ryanheise. Your explication and awesome plugins made my dream project a reality 鉂わ笍

Was this page helpful?
0 / 5 - 0 ratings

Related issues

trighomautumnatg picture trighomautumnatg  路  7Comments

erickcchoi picture erickcchoi  路  6Comments

austinbyron picture austinbyron  路  4Comments

hpuriamanesh picture hpuriamanesh  路  7Comments

lain-ke picture lain-ke  路  6Comments