now works with audio service on android
This commit is contained in:
@@ -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<PlayControls>
|
||||
|
||||
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<PlayControls>
|
||||
|
||||
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<PlayControls>
|
||||
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(
|
||||
|
||||
@@ -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<void> play() async {
|
||||
if (await startAudioSession()) {
|
||||
_player.resume();
|
||||
_player.play(_radioSource, mode: PlayerMode.mediaPlayer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user