state from notification stop now works
This commit is contained in:
@@ -36,8 +36,6 @@ class _PlayControlsState extends State<PlayControls>
|
|||||||
@override
|
@override
|
||||||
bool get wantKeepAlive => true;
|
bool get wantKeepAlive => true;
|
||||||
|
|
||||||
bool isPlaying = false;
|
|
||||||
|
|
||||||
final ListenHandler _listenHandler = getListenHandlder();
|
final ListenHandler _listenHandler = getListenHandlder();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -46,15 +44,8 @@ class _PlayControlsState extends State<PlayControls>
|
|||||||
|
|
||||||
print("Listen init");
|
print("Listen init");
|
||||||
|
|
||||||
_listenHandler.playbackState.listen((PlaybackState event) {
|
_listenHandler.playbackState.listen((PlaybackState state) {
|
||||||
if (isPlaying == event.playing) {
|
setState(() {});
|
||||||
print("State unchanged, skipping setState()");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setState(() {
|
|
||||||
isPlaying = event.playing;
|
|
||||||
print("Updated playing state to $isPlaying");
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,48 +56,33 @@ class _PlayControlsState extends State<PlayControls>
|
|||||||
_listenHandler.stop();
|
_listenHandler.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> playPause() async {
|
||||||
|
print("playPause called");
|
||||||
|
_listenHandler.isPlaying()
|
||||||
|
? await _listenHandler.stop()
|
||||||
|
: await _listenHandler.play();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
super.build(context);
|
super.build(context);
|
||||||
|
|
||||||
print("Running build - Listen");
|
print("Running build - Listen");
|
||||||
|
|
||||||
if (isPlaying) {
|
|
||||||
_listenHandler.play();
|
|
||||||
_listenHandler.playbackState.add(PlaybackState(
|
|
||||||
controls: [
|
|
||||||
MediaControl.stop,
|
|
||||||
MediaControl.pause,
|
|
||||||
],
|
|
||||||
systemActions: {
|
|
||||||
MediaAction.stop,
|
|
||||||
MediaAction.pause,
|
|
||||||
},
|
|
||||||
playing: true,
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
_listenHandler.stop();
|
|
||||||
_listenHandler.playbackState.add(PlaybackState(
|
|
||||||
controls: [],
|
|
||||||
systemActions: {},
|
|
||||||
playing: false,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
return ClipOval(
|
return ClipOval(
|
||||||
child: Material(
|
child: Material(
|
||||||
color: Theme.of(context).colorScheme.primaryContainer,
|
color: Theme.of(context).colorScheme.primaryContainer,
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
splashColor: Theme.of(context).colorScheme.onPrimaryContainer,
|
splashColor: Theme.of(context).colorScheme.onPrimaryContainer,
|
||||||
onTap: () {
|
onTap: () async {
|
||||||
setState(() {
|
await playPause();
|
||||||
isPlaying = !isPlaying;
|
setState(() {});
|
||||||
});
|
|
||||||
},
|
},
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
width: 100,
|
width: 100,
|
||||||
height: 100,
|
height: 100,
|
||||||
child: Icon(
|
child: Icon(
|
||||||
isPlaying ? Icons.stop : Icons.play_arrow,
|
_listenHandler.isPlaying() ? Icons.stop : Icons.play_arrow,
|
||||||
color: Colors.white,
|
color: Colors.white,
|
||||||
size: 50,
|
size: 50,
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -17,15 +17,33 @@ class ListenHandler extends BaseAudioHandler {
|
|||||||
setup_player();
|
setup_player();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isPlaying() => super.playbackState.value.playing;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> play() async {
|
Future<void> play() async {
|
||||||
if (await startAudioSession()) {
|
if (await startAudioSession()) {
|
||||||
_player.play(_radioSource, mode: PlayerMode.mediaPlayer);
|
_player.play(_radioSource, mode: PlayerMode.mediaPlayer);
|
||||||
|
super.playbackState.add(PlaybackState(
|
||||||
|
controls: [
|
||||||
|
MediaControl.stop,
|
||||||
|
MediaControl.pause,
|
||||||
|
],
|
||||||
|
systemActions: {
|
||||||
|
MediaAction.stop,
|
||||||
|
MediaAction.pause,
|
||||||
|
},
|
||||||
|
playing: true,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> pause() async {
|
Future<void> pause() async {
|
||||||
|
super.playbackState.add(PlaybackState(
|
||||||
|
controls: [],
|
||||||
|
systemActions: {},
|
||||||
|
playing: false,
|
||||||
|
));
|
||||||
await stopAudioSession();
|
await stopAudioSession();
|
||||||
_player.stop();
|
_player.stop();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user