audio_service flutter包未启动



我正在尝试使用audio_service包来允许对我的播客播放应用程序进行后台控制。服务未启动。当我调用AudioService.start()时,不会返回任何内容(我正在await调用此函数,并试图在返回后打印一条语句,但它永远不会返回。此外,当我第一次尝试播放某个项目时,它会正确地将AudioService.running求值为false,然后尝试启动AudioService(挂在start()函数上(。当我第二次点击播放时,它会将AudioService.running评估为true(即使启动函数从未返回(,然后会抛出以下错误:

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)

这是我调用启动函数的代码:

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');
}
}

后台任务入口点的代码(它在任何其他类之外,因为它不在我在另一期文章中读到的必须是顶级的类中工作(:

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

最后,我的BackgroundAudioTask中的onStart中似乎没有任何代码正在运行(因为onStart顶部的打印语句没有运行(。所以我不确定这里发生了什么。它并没有给我一个明显的错误,也没有给我任何关于从哪里开始调试audio_service的提示。

在启动服务之前调用AudioService.connect();将解决错误。

对我来说,AudioService.start()将冻结,直到我根据audio_service README文件将WAKE_LOCKFOREGROUND_SERVICE权限添加到AndroidManifest.xml文件。

最新更新