Reputation: 6820
So I have followed the tutorial https://github.com/ryanheise/audio_service/wiki/Tutorial
However I have not had any success getting it to work.
My first issues was
final _completer = Completer();
Their is no Completer class so that throw an error.
And it seems that the code they provide just does not fire the following class.
/*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();
}
}
The full code is
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();
}
}
Upvotes: 1
Views: 3352
Reputation: 96
I followed the document we must add the method _backgroundTaskEntrypoint() on the top level function, so you should move the function outside the class.
// Must be a top-level function
void _backgroundTaskEntrypoint() {
AudioServiceBackground.run(() => AudioPlayerTask());
}
/*AUDIO PLAYER*/
class AudioPlayerTask extends BackgroundAudioTask {
final _audioPlayer = AudioPlayer();
...
}
Upvotes: 2
Reputation: 121
It's not working for me either. Note that to use audio_service on Android you must put the following lines into AndroidManifest.xml. However, when I do so, com.ryanheise.audioservice.MediaButtonReceiver gives an error "Class referenced in the manifest, com.ryanheise.audioservice.AudioService, was not found in the project or the libraries", and I'm not sure what to do about that.
This needs to go into the Manifest. See the documentation.
<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>
Upvotes: 0