59 lines
1.4 KiB
Dart
59 lines
1.4 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:just_audio_background/just_audio_background.dart';
|
|
import 'package:just_audio/just_audio.dart';
|
|
|
|
class StreamingAudioBusinessObject extends ChangeNotifier {
|
|
StreamingAudioBusinessObject({
|
|
this.streamUrl = 'https://kryz.out.airtime.pro/kryz_a',
|
|
}) {
|
|
_playerStateSubscription = _player.playerStateStream.listen((state) {
|
|
final isCurrentlyPlaying =
|
|
state.playing && state.processingState != ProcessingState.completed;
|
|
|
|
if (_isPlaying != isCurrentlyPlaying) {
|
|
_isPlaying = isCurrentlyPlaying;
|
|
notifyListeners();
|
|
}
|
|
});
|
|
}
|
|
|
|
final String streamUrl;
|
|
final AudioPlayer _player = AudioPlayer();
|
|
late final StreamSubscription<PlayerState> _playerStateSubscription;
|
|
bool _isSourceLoaded = false;
|
|
bool _isPlaying = false;
|
|
|
|
bool get isPlaying => _isPlaying;
|
|
|
|
Future<void> play() async {
|
|
if (!_isSourceLoaded) {
|
|
await _player.setAudioSource(
|
|
AudioSource.uri(
|
|
Uri.parse(streamUrl),
|
|
tag: const MediaItem(
|
|
id: 'kryz-live-stream',
|
|
title: 'KRYZ Live Stream',
|
|
artist: 'KRYZ Radio',
|
|
),
|
|
),
|
|
);
|
|
_isSourceLoaded = true;
|
|
}
|
|
|
|
await _player.play();
|
|
}
|
|
|
|
Future<void> pause() async {
|
|
await _player.pause();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_playerStateSubscription.cancel();
|
|
_player.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|