部分界面调整,把底部的播放控件完整前置
This commit is contained in:
@@ -1,57 +1,91 @@
|
||||
// lib/services/playback_service.dart
|
||||
import 'package:media_kit/media_kit.dart';
|
||||
|
||||
// ============================================================
|
||||
// PlaybackService - 播放引擎封装
|
||||
// 职责:管理 Player 生命周期,不涉及业务逻辑
|
||||
// ============================================================
|
||||
class PlaybackService {
|
||||
static final PlaybackService _instance = PlaybackService._();
|
||||
static final PlaybackService _instance = PlaybackService._internal();
|
||||
factory PlaybackService() => _instance;
|
||||
PlaybackService._();
|
||||
PlaybackService._internal();
|
||||
|
||||
late final Player _player;
|
||||
Player? _player;
|
||||
bool _initialized = false;
|
||||
|
||||
// 获取 Player 实例(供 AudioService 直接监听)
|
||||
// ---------- Getter ----------
|
||||
Player get player {
|
||||
if (!_initialized) init();
|
||||
return _player;
|
||||
return _player!;
|
||||
}
|
||||
|
||||
bool get isInitialized => _initialized;
|
||||
|
||||
// ---------- 初始化 ----------
|
||||
void init() {
|
||||
if (_initialized) return;
|
||||
_player = Player();
|
||||
_initialized = true;
|
||||
|
||||
// 可以在这里配置初始参数
|
||||
// _player.setVolume(1.0);
|
||||
}
|
||||
|
||||
// ---------- 播放 ----------
|
||||
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
|
||||
|
||||
final media = (headers != null && headers.isNotEmpty)
|
||||
? Media(url, httpHeaders: headers)
|
||||
: Media(url);
|
||||
await _player.open(media);
|
||||
await _player.play();
|
||||
|
||||
await _player!.open(media);
|
||||
await _player!.play();
|
||||
}
|
||||
|
||||
// ---------- 控制 ----------
|
||||
void pause() {
|
||||
if (_initialized) _player.pause();
|
||||
if (_initialized && _player != null) {
|
||||
_player!.pause();
|
||||
}
|
||||
}
|
||||
|
||||
void resume() {
|
||||
if (_initialized) _player.play();
|
||||
if (_initialized && _player != null) {
|
||||
_player!.play();
|
||||
}
|
||||
}
|
||||
|
||||
void stop() {
|
||||
if (_initialized) _player.stop();
|
||||
if (_initialized && _player != null) {
|
||||
_player!.stop();
|
||||
}
|
||||
}
|
||||
|
||||
void seek(Duration position) {
|
||||
if (_initialized) _player.seek(position);
|
||||
if (_initialized && _player != null) {
|
||||
_player!.seek(position);
|
||||
}
|
||||
}
|
||||
|
||||
void setVolume(double volume) {
|
||||
if (_initialized && _player != null) {
|
||||
_player!.setVolume(volume);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 释放 ----------
|
||||
void dispose() {
|
||||
if (_initialized) {
|
||||
_player.dispose();
|
||||
if (_initialized && _player != null) {
|
||||
_player!.dispose();
|
||||
_player = null;
|
||||
_initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 重新初始化(用于 Hot Restart 场景) ----------
|
||||
void reinitialize() {
|
||||
dispose();
|
||||
init();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user