暂时把bug先处理掉了,准备尝试实现最小播放目标
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import 'package:media_kit/media_kit.dart';
|
||||
import 'package:media_kit/media_kit.dart' as media_kit;
|
||||
|
||||
class PlaybackService {
|
||||
static final PlaybackService _instance = PlaybackService._();
|
||||
@@ -7,22 +6,61 @@ class PlaybackService {
|
||||
PlaybackService._();
|
||||
|
||||
late final Player _player;
|
||||
bool get isInitialized => _player.state.playing;
|
||||
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() => _player.pause();
|
||||
void resume() => _player.play();
|
||||
void stop() => _player.stop();
|
||||
void dispose() => _player.dispose();
|
||||
void pause() {
|
||||
if (_initialized) {
|
||||
_player.pause();
|
||||
}
|
||||
}
|
||||
|
||||
// 状态流
|
||||
Stream<PlayerState> get stateStream => _player.stream;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user