From a384c05a63d538322cf59eeffb6f685ded2a4885 Mon Sep 17 00:00:00 2001 From: David Senk Date: Wed, 18 Sep 2024 17:06:02 -0400 Subject: [PATCH] now works with audio service on android --- lib/Listen.dart | 85 +++++++++++++++++------------------------- lib/ListenHandler.dart | 11 +++--- 2 files changed, 39 insertions(+), 57 deletions(-) diff --git a/lib/Listen.dart b/lib/Listen.dart index 040a484..12b3127 100644 --- a/lib/Listen.dart +++ b/lib/Listen.dart @@ -1,6 +1,9 @@ -import 'package:audioplayers/audioplayers.dart'; +import 'package:audio_service/audio_service.dart'; import 'package:flutter/material.dart'; +import 'Globals.dart'; +import 'ListenHandler.dart'; + class Listen extends StatelessWidget { const Listen({super.key}); @@ -26,13 +29,7 @@ class _PlayControlsState extends State bool isPlaying = false; - late final AudioPlayer player; - - //final ListenHandler _listenHandler = getListenHandlder(); - - final UrlSource _radioSource = UrlSource( - "https://generic.ything.app/music/separation-185196.mp3", - mimeType: "audio/mpeg"); + final ListenHandler _listenHandler = getListenHandlder(); @override initState() { @@ -40,34 +37,23 @@ 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"); - // }); - //}); - - player = AudioPlayer(); - - player.setReleaseMode(ReleaseMode.release); - - //player - // .setSourceUrl("https://generic.ything.app/music/separation-185196.mp3"); + _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"); + }); + }); } @override void dispose() { super.dispose(); - //player.stop(); - - //player.dispose(); - - //_listenHandler.stop(); + _listenHandler.stop(); } @override @@ -77,28 +63,25 @@ class _PlayControlsState extends State print("Running build - Listen"); if (isPlaying) { - player.play(_radioSource, mode: PlayerMode.mediaPlayer); - //player.resume(); - //_listenHandler.play(); - //_listenHandler.playbackState.add(PlaybackState( - // controls: [ - // MediaControl.stop, - // MediaControl.pause, - // ], - // systemActions: { - // MediaAction.stop, - // MediaAction.pause, - // }, - // playing: true, - //)); + _listenHandler.play(); + _listenHandler.playbackState.add(PlaybackState( + controls: [ + MediaControl.stop, + MediaControl.pause, + ], + systemActions: { + MediaAction.stop, + MediaAction.pause, + }, + playing: true, + )); } else { - player.stop(); - //_listenHandler.stop(); - //_listenHandler.playbackState.add(PlaybackState( - // controls: [], - // systemActions: {}, - // playing: false, - //)); + _listenHandler.stop(); + _listenHandler.playbackState.add(PlaybackState( + controls: [], + systemActions: {}, + playing: false, + )); } return ClipOval( child: Material( diff --git a/lib/ListenHandler.dart b/lib/ListenHandler.dart index 8891b0f..a731717 100644 --- a/lib/ListenHandler.dart +++ b/lib/ListenHandler.dart @@ -5,13 +5,12 @@ import 'package:ything_radio/Globals.dart'; class ListenHandler extends BaseAudioHandler { final _player = AudioPlayer(); + final UrlSource _radioSource = UrlSource( + "https://generic.ything.app/music/separation-185196.mp3", + mimeType: "audio/mpeg"); + setup_player() { _player.setReleaseMode(ReleaseMode.release); - - _player.setSourceUrl( - "https://generic.ything.app/music/separation-185196.mp3", - mimeType: "audio/mpeg", - ); } ListenHandler() { @@ -21,7 +20,7 @@ class ListenHandler extends BaseAudioHandler { @override Future play() async { if (await startAudioSession()) { - _player.resume(); + _player.play(_radioSource, mode: PlayerMode.mediaPlayer); } }