Skip to content

Instantly share code, notes, and snippets.

@BeMacized
Created January 1, 2019 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BeMacized/5ccc11f43c4b18bd1e40a1e539082721 to your computer and use it in GitHub Desktop.
Save BeMacized/5ccc11f43c4b18bd1e40a1e539082721 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:audio_service/audio_service.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
connect();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
connect();
break;
case AppLifecycleState.paused:
AudioService.disconnect();
break;
default:
break;
}
}
void connect() {
AudioService.connect();
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Audio Service Demo'),
),
body: Center(
child: RaisedButton(
child: Text('ADD TO QUEUE'),
onPressed: () async {
await AudioService.start(
backgroundTask: _backgroundAudioPlayerTask,
resumeOnClick: true,
notificationChannelName: 'Audio Service Demo',
notificationColor: 0xFF2196f3,
androidNotificationIcon: 'mipmap/ic_launcher',
);
MediaItem mediaItem = MediaItem(
id: 'audio_1', album: 'Sample Album', title: 'Sample Title');
AudioService.addQueueItem(mediaItem);
},
),
),
),
);
}
}
void _backgroundAudioPlayerTask() async {
AudioServiceBackground.run(
onStart: () {},
onPlay: () {},
onPause: () {},
onStop: () {},
onAddQueueItem: (String mediaId) {
print("Queued: " + mediaId);
},
onClick: (MediaButton button) {},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment