49 lines
1.2 KiB
Dart
49 lines
1.2 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/foundation.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.setUrl(streamUrl);
|
|
_isSourceLoaded = true;
|
|
}
|
|
|
|
await _player.play();
|
|
}
|
|
|
|
Future<void> pause() async {
|
|
await _player.pause();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_playerStateSubscription.cancel();
|
|
_player.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|