Kyle B.
Kyle B.

Reputation: 439

audio_service flutter package does not start

I am trying to use the audio_service package to allow background controls for my podcast playing app. The service does not start. When I call AudioService.start(), nothing is ever returned (I'm awaiting this function and trying to print a statement after this returns, but it never returns. Also, when I first attempt to play an item, it will correctly evaluate AudioService.running to false and then try to start the AudioService (hanging on the start() function). When I hit play a second time, it will evaluate AudioService.running as true (even though the start function never returned) and will then throw this error:

E/flutter ( 8726): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: PlatformException(Attempt to invoke virtual method 'void io.flutter.plugin.common.MethodChannel.invokeMethod(java.lang.String, java.lang.Object, io.flutter.plugin.common.MethodChannel$Result)' on a null object reference, null, null)
E/flutter ( 8726): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:572:7)
E/flutter ( 8726): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:161:18)
E/flutter ( 8726): <asynchronous suspension>
E/flutter ( 8726): #2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:334:12)
E/flutter ( 8726): #3      AudioService.play (package:audio_service/audio_service.dart:895:20)

Here is my code which calls the start function:

    if(AudioService.running) {
      print('playing audio service: ${AudioService.running}');
      AudioService.play();
    } else {
      MediaItem mediaItem = MediaItem(
        id: newPostPod.audioUrl,
        title: newPostPod.titleTextString(),
        artist: newPostPod.subtitleTextString(),
      );
      print('starting audio service');
      bool started = await AudioService.start(
        backgroundTaskEntrypoint: _backgroundTaskEntrypoint,
        params: {'mediaItem': mediaItem.toJson()},
      );
      print('audio service started');
      if(AudioService.running) {
        print('starting to play via media service');
        AudioService.playMediaItem(mediaItem);
      } else {
        print('Audio Service not yet started: $started');
      }
    }

The code for the background task entrypoint (which is outside of any other class, as it wasn't working inside a class I had read in another issue that it had to be top-level):

_backgroundTaskEntrypoint() {
  AudioServiceBackground.run(() => PlayerTask());
}

Lastly, it doesn't appear any of the code within my onStart in my BackgroundAudioTask is running (as print statements at the top of my onStart are not being run). So I'm not sure what's happening here. It isn't give me a clear error or really any hint of where to start debugging audio_service.

Upvotes: 2

Views: 2401

Answers (2)

Pete Alvin
Pete Alvin

Reputation: 4790

For me AudioService.start() would freeze until I added the WAKE_LOCK and FOREGROUND_SERVICE permissions to the AndroidManifest.xml file per the audio_service README file.

Upvotes: 1

Henok
Henok

Reputation: 3383

Calling AudioService.connect(); before starting the service will solve the error.

Upvotes: 2

Related Questions