Created
January 1, 2019 21:24
-
-
Save BeMacized/5ccc11f43c4b18bd1e40a1e539082721 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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