所以我遵循了教程https://github.com/ryanheise/audio_service/wiki/Tutorial
但是,我无法成功使其正常工作。
我的第一个问题是
final _completer = Completer();
他们不是Completer类,因此会引发错误。
似乎他们提供的代码不会触发以下类。
/*AUDIO PLAYER*/
class AudioPlayerTask extends BackgroundAudioTask {
final _audioPlayer = AudioPlayer();
// final _completer = Completer();
@override
Future<void> onStart(Map<String, dynamic> params) async {
// Connect to the URL
print("test");
await _audioPlayer.setUrl("https://perth.adstichr.com.au/station/DRN1?uuid=0000-0000-0000-0000");
// Now we're ready to play
_audioPlayer.play();
}
@override
Future<void> onStop() async {
// Stop playing audio
await _audioPlayer.stop();
// Shut down this background task
await super.onStop();
}
}
完整代码是
import 'package:flutter/material.dart';
import 'package:audio_service/audio_service.dart';
import 'package:just_audio/just_audio.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: AudioServiceWidget(child: MainScreen()),
);
}
}
class MainScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Example")),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(child: Text("Start"), onPressed: start),
RaisedButton(child: Text("Stop"), onPressed: stop),
],
),
),
);
}
start() =>
AudioService.start(backgroundTaskEntrypoint: _backgroundTaskEntrypoint);
stop() => AudioService.stop();
_backgroundTaskEntrypoint() {
AudioServiceBackground.run(() => AudioPlayerTask());
}
}
/*AUDIO PLAYER*/
class AudioPlayerTask extends BackgroundAudioTask {
final _audioPlayer = AudioPlayer();
// final _completer = Completer();
@override
Future<void> onStart(Map<String, dynamic> params) async {
// Connect to the URL
print("test");
await _audioPlayer.setUrl("https://perth.adstichr.com.au/station/DRN1?uuid=0000-0000-0000-0000");
// Now we're ready to play
_audioPlayer.play();
}
@override
Future<void> onStop() async {
// Stop playing audio
await _audioPlayer.stop();
// Shut down this background task
await super.onStop();
}
}
答案 0 :(得分:0)
对我也不起作用。请注意,要在Android上使用audio_service,必须将以下行放入AndroidManifest.xml中。但是,当我这样做时,com.ryanheise.audioservice.MediaButtonReceiver给出错误“在项目或库中未找到清单com.ryanheise.audioservice.AudioService中引用的类”,并且我不确定为此。
这需要进入清单。请参阅文档。
<manifest ...>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application ...>
...
<service android:name="com.ryanheise.audioservice.AudioService">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
<receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
</application>
</manifest>
答案 1 :(得分:0)
我遵循文档,必须在顶级函数上添加方法 _backgroundTaskEntrypoint(),因此您应将函数移到类之外。
// Must be a top-level function
void _backgroundTaskEntrypoint() {
AudioServiceBackground.run(() => AudioPlayerTask());
}
/*AUDIO PLAYER*/
class AudioPlayerTask extends BackgroundAudioTask {
final _audioPlayer = AudioPlayer();
...
}