Audio_service: How to pass que from api to background audio task?

Created on 20 May 2020  ·  8Comments  ·  Source: ryanheise/audio_service

Checklist

  • [x] The question is not already in the FAQ.
  • [ ] The question is not too narrow or specific to a particular application.

Suggested Question

How to you pass a queue from API to Background audio task?

1 backlog question

Most helpful comment

Please thank @getmmg (the person who gave you the help).

When you say "It doesn't work", can you be very specific about what didn't work? Do you mean that onReplaceQueue isn't being called at all? If you insert the following in your onReplaceQueue callback:

print(queue);

does it successfully print out the queue? If so, then it is behaving as per its documentation. Beyond that, it's up to you how you implement this callback method. It will "work" the way you implement it to work, and remember that none of the callbacks do anything by default, so it's up to you to make them work according to what you want to happen (see the tutorial for an introduction to callbacks in general).

On the other hand, if the print output from that print statement doesn't appear, then it is probably a bug in this plugin and you may need to open a bug report.

All 8 comments

AudioService.replaceQueue, AudioService.addQueueItems methods. Both take the list of MediaItem

Thank you.
Can you please give me an example of how to do this after I call the AudioService.start and also how to play a particular index. I will really appreciate it.

@Waxxymwansa :

When you start the audio service Make sure to pass enableQueue: true,

Then you can pass the queue to backend using AudioService.replaceQueue or AudioService.addQueueItems methods.

Then you can play an item using AudioService.skipToQueueItem which would call
void onSkipToQueueItem(String mediaId) on backend. you can override this method to play the mediaitem using the mediaId.

This is only one way of doing it, you can do it in plenty other ways I would suggest you to look at the source code for all the available methods, everything is explained in detail. Also there is an example in the Wiki Section. You can refer that one for detailed example.

@ryanheise
Thank you for the assistance and it worked well.
Though when I call
AudioService.addQueueItems
It adds to the current list and doesn't reset the list.

AudioService.replaceQueue
It doesn't work for me. Is there something I am missing when I use this method?

This is my implementation in the UI
await AudioService.replaceQueue(model.songs); await AudioService.skipToQueueItem(model.currentSong.id);

then in the background service class, I call


@override
  Future<void> onSkipToQueueItem(String mediaId) async {
    _queueIndex = _queue.indexWhere((m) => m.id == mediaId);
    AudioServiceBackground.setMediaItem(_queue[_queueIndex]);
    await _audioPlayer.setUrl(mediaItem.id);
    _skipState = null;
    onPlay();
    super.onSkipToQueueItem(mediaId);
  }
  @override
  Future<void> onReplaceQueue(List<MediaItem> queue) async {
    super.onReplaceQueue(queue);
    await AudioServiceBackground.setQueue(queue);
  }

I want to be able to replace the entire list and not add to it when I click on an item in my list as I have multiple lists.

Your response will be greatly appreciated.

Please thank @getmmg (the person who gave you the help).

When you say "It doesn't work", can you be very specific about what didn't work? Do you mean that onReplaceQueue isn't being called at all? If you insert the following in your onReplaceQueue callback:

print(queue);

does it successfully print out the queue? If so, then it is behaving as per its documentation. Beyond that, it's up to you how you implement this callback method. It will "work" the way you implement it to work, and remember that none of the callbacks do anything by default, so it's up to you to make them work according to what you want to happen (see the tutorial for an introduction to callbacks in general).

On the other hand, if the print output from that print statement doesn't appear, then it is probably a bug in this plugin and you may need to open a bug report.

@Waxxymwansa

Try setting the _queue variable with the one passed to the method.
_queue= queue

 @override
  Future<void> onReplaceQueue(List<MediaItem> queue) async {
    super.onReplaceQueue(queue);
   _queue= queue
    await AudioServiceBackground.setQueue(queue);
  }

Thanks so much this was a great help.
I will definitely be sure to send a copy of my app when it's done 🙏

Hi, I'm also trying to update the queue by calling Audioservice.updateQueue but I'm getting a NullPointerException

E/flutter ( 5634): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'void com.ryanheise.audioservice.AudioServicePlugin$BackgroundHandler.invokeMethod(io.flutter.plugin.common.MethodChannel$Result, java.lang.String, java.lang.Object[])' on a null object reference, null) E/flutter ( 5634): #0 StandardMethodCodec.decodeEnvelope package:flutter/…/services/message_codecs.dart:569 E/flutter ( 5634): #1 MethodChannel._invokeMethod package:flutter/…/services/platform_channel.dart:156 E/flutter ( 5634): <asynchronous suspension> E/flutter ( 5634): #2 MethodChannel.invokeMethod package:flutter/…/services/platform_channel.dart:329 E/flutter ( 5634): #3 AudioService.updateQueue package:audio_service/audio_service.dart:744 E/flutter ( 5634): #4 EpisodeDetail.build.<anonymous closure> package:cettalks1/screens/episodedetail.dart:52 E/flutter ( 5634): #5 _InkResponseState._handleTap package:flutter/…/material/ink_well.dart:779 E/flutter ( 5634): #6 _InkResponseState.build.<anonymous closure> package:flutter/…/material/ink_well.dart:862 E/flutter ( 5634): #7 GestureRecognizer.invokeCallback package:flutter/…/gestures/recognizer.dart:182 E/flutter ( 5634): #8 TapGestureRecognizer.handleTapUp package:flutter/…/gestures/tap.dart:504 E/flutter ( 5634): #9 BaseTapGestureRecognizer._checkUp package:flutter/…/gestures/tap.dart:282 E/flutter ( 5634): #10 BaseTapGestureRecognizer.handlePrimaryPointer package:flutter/…/gestures/tap.dart:217 E/flutter ( 5634): #11 PrimaryPointerGestureRecognizer.handleEvent package:flutter/…/gestures/recognizer.dart:475 E/flutter ( 5634): #12 PointerRouter._dispatch package:flutter/…/gestures/pointer_router.dart:76 E/flutter ( 5634): #13 PointerRouter._dispatchEventToRoutes.<anonymous closure> package:flutter/…/gestures/pointer_router.dart:122 E/flutter ( 5634): #14 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8) E/flutter ( 5634): #15 PointerRouter._dispatchEventToRoutes package:flutter/…/gestures/pointer_router.dart:120 E/flutter ( 5634): #16 PointerRouter.route package:flutter/…/gestures/pointer_router.dart:106 E/flutter ( 5634): #17 GestureBinding.handleEvent package:flutter/…/gestures/binding.dart:218 E/flutter ( 5634): #18 GestureBinding.dispatchEvent package:flutter/…/gestures/binding.dart:198 E/flutter ( 5634): #19 GestureBinding._handlePointerEvent package:flutter/…/gestures/binding.dart:156 E//flutter ( 5634): #20 GestureBinding._flushPointerEventQueue package:flutter/…/gestures/binding.dart:102 E/flutter ( 5634): #21 GestureBinding._handlePointerDataPacket package:flutter/…/gestures/binding.dart:86 E/flutter ( 5634): #22 _rootRunUnary (dart:async/zone.dart:1196:13) E/flutter ( 5634): #23 _CustomZone.runUnary (dart:async/zone.dart:1085:19) E/flutter ( 5634): #24 _CustomZone.runUnaryGuarded (dart:async/zone.dart:987:7) E/flutter ( 5634): #25 _invoke1 (dart:ui/hooks.dart:275:10) E/flutter ( 5634): #26 _dispatchPointerDataPacket (dart:ui/hooks.dart:184:5) E/flutter ( 5634):

Any help is appreciated

Was this page helpful?
0 / 5 - 0 ratings