From 137f31c1048b27f074ce9f3513afe3ade184ed75 Mon Sep 17 00:00:00 2001 From: David Senk Date: Thu, 19 Sep 2024 20:02:58 -0400 Subject: [PATCH] state from notification stop now works --- lib/Listen.dart | 50 +++++++++++------------------------------- lib/ListenHandler.dart | 18 +++++++++++++++ 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/lib/Listen.dart b/lib/Listen.dart index 15c8689..f0b1771 100644 --- a/lib/Listen.dart +++ b/lib/Listen.dart @@ -36,8 +36,6 @@ class _PlayControlsState extends State @override bool get wantKeepAlive => true; - bool isPlaying = false; - final ListenHandler _listenHandler = getListenHandlder(); @override @@ -46,15 +44,8 @@ class _PlayControlsState extends State print("Listen init"); - _listenHandler.playbackState.listen((PlaybackState event) { - if (isPlaying == event.playing) { - print("State unchanged, skipping setState()"); - return; - } - setState(() { - isPlaying = event.playing; - print("Updated playing state to $isPlaying"); - }); + _listenHandler.playbackState.listen((PlaybackState state) { + setState(() {}); }); } @@ -65,48 +56,33 @@ class _PlayControlsState extends State _listenHandler.stop(); } + Future playPause() async { + print("playPause called"); + _listenHandler.isPlaying() + ? await _listenHandler.stop() + : await _listenHandler.play(); + } + @override Widget build(BuildContext context) { super.build(context); 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( child: Material( color: Theme.of(context).colorScheme.primaryContainer, child: InkWell( splashColor: Theme.of(context).colorScheme.onPrimaryContainer, - onTap: () { - setState(() { - isPlaying = !isPlaying; - }); + onTap: () async { + await playPause(); + setState(() {}); }, child: SizedBox( width: 100, height: 100, child: Icon( - isPlaying ? Icons.stop : Icons.play_arrow, + _listenHandler.isPlaying() ? Icons.stop : Icons.play_arrow, color: Colors.white, size: 50, ), diff --git a/lib/ListenHandler.dart b/lib/ListenHandler.dart index a731717..902981f 100644 --- a/lib/ListenHandler.dart +++ b/lib/ListenHandler.dart @@ -17,15 +17,33 @@ class ListenHandler extends BaseAudioHandler { setup_player(); } + bool isPlaying() => super.playbackState.value.playing; + @override Future play() async { if (await startAudioSession()) { _player.play(_radioSource, mode: PlayerMode.mediaPlayer); + super.playbackState.add(PlaybackState( + controls: [ + MediaControl.stop, + MediaControl.pause, + ], + systemActions: { + MediaAction.stop, + MediaAction.pause, + }, + playing: true, + )); } } @override Future pause() async { + super.playbackState.add(PlaybackState( + controls: [], + systemActions: {}, + playing: false, + )); await stopAudioSession(); _player.stop(); }