58 lines
1.2 KiB
Dart
58 lines
1.2 KiB
Dart
// lib/services/playback_service.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;
|
|
|
|
// 获取 Player 实例(供 AudioService 直接监听)
|
|
Player get player {
|
|
if (!_initialized) init();
|
|
return _player;
|
|
}
|
|
|
|
void init() {
|
|
if (_initialized) return;
|
|
_player = Player();
|
|
_initialized = true;
|
|
}
|
|
|
|
Future<void> play(String url, {Map<String, String>? headers}) async {
|
|
if (!_initialized) init();
|
|
print('🎵 播放 URL: $url');
|
|
print('📋 认证头: ${headers?.keys}');
|
|
final media = headers != null && headers.isNotEmpty
|
|
? Media(url, httpHeaders: headers)
|
|
: Media(url);
|
|
await _player.open(media);
|
|
await _player.play();
|
|
}
|
|
|
|
void pause() {
|
|
if (_initialized) _player.pause();
|
|
}
|
|
|
|
void resume() {
|
|
if (_initialized) _player.play();
|
|
}
|
|
|
|
void stop() {
|
|
if (_initialized) _player.stop();
|
|
}
|
|
|
|
void seek(Duration position) {
|
|
if (_initialized) _player.seek(position);
|
|
}
|
|
|
|
void dispose() {
|
|
if (_initialized) {
|
|
_player.dispose();
|
|
_initialized = false;
|
|
}
|
|
}
|
|
}
|