67 lines
1.2 KiB
Dart
67 lines
1.2 KiB
Dart
import 'package:media_kit/media_kit.dart';
|
|
|
|
class PlaybackService {
|
|
static final PlaybackService _instance = PlaybackService._();
|
|
factory PlaybackService() => _instance;
|
|
PlaybackService._();
|
|
|
|
late final Player _player;
|
|
bool _initialized = false;
|
|
|
|
void init() {
|
|
if (_initialized) return;
|
|
_player = Player();
|
|
_initialized = true;
|
|
}
|
|
|
|
Future<void> play(String url) async {
|
|
if (!_initialized) init();
|
|
await _player.open(Media(url));
|
|
await _player.play();
|
|
}
|
|
|
|
void pause() {
|
|
if (_initialized) {
|
|
_player.pause();
|
|
}
|
|
}
|
|
|
|
void resume() {
|
|
if (_initialized) {
|
|
_player.play();
|
|
}
|
|
}
|
|
|
|
void stop() {
|
|
if (_initialized) {
|
|
_player.stop();
|
|
}
|
|
}
|
|
|
|
void dispose() {
|
|
if (_initialized) {
|
|
_player.dispose();
|
|
_initialized = false;
|
|
}
|
|
}
|
|
|
|
// ✅ 直接返回 PlayerStream(它就是播放器的状态流)
|
|
PlayerStream get stateStream {
|
|
if (!_initialized) init();
|
|
return _player.stream;
|
|
}
|
|
|
|
// 获取当前播放状态
|
|
PlayerState get currentState {
|
|
if (!_initialized) {
|
|
return PlayerState(
|
|
playing: false,
|
|
position: Duration.zero,
|
|
duration: Duration.zero,
|
|
buffering: false,
|
|
);
|
|
}
|
|
return _player.state;
|
|
}
|
|
}
|