diff --git a/lib/Globals.dart b/lib/Globals.dart index 44a3da2..4076638 100644 --- a/lib/Globals.dart +++ b/lib/Globals.dart @@ -1,11 +1,28 @@ import 'package:audio_service/audio_service.dart'; import 'package:audio_session/audio_session.dart'; +import 'package:audioplayers/audioplayers.dart'; +import 'package:http/http.dart' as http; import 'ListenHandler.dart'; late final ListenHandler _listenHandler; late final AudioSession _session; +final String _fallback = + "https://generic.ything.app/music/separation-185196.mp3"; +final _urlSource = Uri.parse("https://generic.ything.app/music/player.url"); +late final String _radioUrl; + +Future loadCurrentUrl() async { + final resp = await http.get(_urlSource); + if (resp.statusCode == 200) { + _radioUrl = resp.body.trim(); + } else { + _radioUrl = _fallback; + print('Request for current streaming url failed'); + } +} + Future setupListenHandler() async { _listenHandler = await AudioService.init( builder: () => ListenHandler(), @@ -51,6 +68,10 @@ Future setupListenHandler() async { ListenHandler getListenHandlder() => _listenHandler; +UrlSource getUrlSource() { + return UrlSource(_radioUrl, mimeType: "audio/mpeg"); +} + Future startAudioSession() async { return await _session.setActive(true); } diff --git a/lib/ListenHandler.dart b/lib/ListenHandler.dart index 8357f77..f607745 100644 --- a/lib/ListenHandler.dart +++ b/lib/ListenHandler.dart @@ -5,9 +5,7 @@ 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"); + final UrlSource _radioSource = getUrlSource(); setup_player() { _player.setReleaseMode(ReleaseMode.release); diff --git a/lib/main.dart b/lib/main.dart index 16e8c9e..7f81f87 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -4,6 +4,7 @@ import 'Globals.dart'; import 'YthingRadio.dart'; Future main() async { + await loadCurrentUrl(); await setupListenHandler(); runApp(const YthingRadio()); } diff --git a/pubspec.lock b/pubspec.lock index 876fafc..4b5487c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -273,7 +273,7 @@ packages: source: hosted version: "7.7.0" http: - dependency: transitive + dependency: "direct main" description: name: http sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010 diff --git a/pubspec.yaml b/pubspec.yaml index 125774e..39edca6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -40,6 +40,7 @@ dependencies: flutter_tabler_icons: ^1.38.0 audio_service: ^0.18.15 audio_session: ^0.1.21 + http: ^1.2.2 dev_dependencies: flutter_test: