opt: 播放器单例

This commit is contained in:
guozhigq
2024-05-15 23:55:04 +08:00
parent 5a10dbaf4c
commit 44f2a168ce
7 changed files with 21 additions and 23 deletions

View File

@ -17,8 +17,7 @@ class LiveRoomController extends GetxController {
double volume = 0.0; double volume = 0.0;
// 静音状态 // 静音状态
RxBool volumeOff = false.obs; RxBool volumeOff = false.obs;
PlPlayerController plPlayerController = PlPlayerController plPlayerController = PlPlayerController(videoType: 'live');
PlPlayerController.getInstance(videoType: 'live');
Rx<RoomInfoH5Model> roomInfoH5 = RoomInfoH5Model().obs; Rx<RoomInfoH5Model> roomInfoH5 = RoomInfoH5Model().obs;
late bool enableCDN; late bool enableCDN;
late int currentQn; late int currentQn;

View File

@ -74,7 +74,7 @@ class VideoDetailController extends GetxController
final scaffoldKey = GlobalKey<ScaffoldState>(); final scaffoldKey = GlobalKey<ScaffoldState>();
RxString bgCover = ''.obs; RxString bgCover = ''.obs;
RxString cover = ''.obs; RxString cover = ''.obs;
PlPlayerController plPlayerController = PlPlayerController.getInstance(); PlPlayerController plPlayerController = PlPlayerController();
late VideoItem firstVideo; late VideoItem firstVideo;
late AudioItem firstAudio; late AudioItem firstAudio;

View File

@ -123,6 +123,7 @@ class PlPlayerController {
PreferredSizeWidget? bottomControl; PreferredSizeWidget? bottomControl;
Widget? danmuWidget; Widget? danmuWidget;
late RxList subtitles; late RxList subtitles;
String videoType = 'archive';
/// 数据加载监听 /// 数据加载监听
Stream<DataStatus> get onDataStatusChanged => dataStatus.status.stream; Stream<DataStatus> get onDataStatusChanged => dataStatus.status.stream;
@ -220,7 +221,7 @@ class PlPlayerController {
Rx<int> get playerCount => _playerCount; Rx<int> get playerCount => _playerCount;
/// ///
Rx<String> get videoType => _videoType; // Rx<String> get videoType => _videoType;
/// 弹幕开关 /// 弹幕开关
Rx<bool> isOpenDanmu = false.obs; Rx<bool> isOpenDanmu = false.obs;
@ -274,8 +275,7 @@ class PlPlayerController {
} }
// 添加一个私有构造函数 // 添加一个私有构造函数
PlPlayerController._() { PlPlayerController._internal(this.videoType) {
_videoType = videoType;
isOpenDanmu.value = isOpenDanmu.value =
setting.get(SettingBoxKey.enableShowDanmaku, defaultValue: false); setting.get(SettingBoxKey.enableShowDanmaku, defaultValue: false);
blockTypes = blockTypes =
@ -330,11 +330,11 @@ class PlPlayerController {
} }
// 获取实例 传参 // 获取实例 传参
static PlPlayerController getInstance({ factory PlPlayerController({
String videoType = 'archive', String videoType = 'archive',
}) { }) {
// 如果实例尚未创建,则创建一个新实例 // 如果实例尚未创建,则创建一个新实例
_instance ??= PlPlayerController._(); _instance ??= PlPlayerController._internal(videoType);
if (videoType != 'none') { if (videoType != 'none') {
_instance!._playerCount.value += 1; _instance!._playerCount.value += 1;
_videoType.value = videoType; _videoType.value = videoType;
@ -443,7 +443,7 @@ class PlPlayerController {
configuration: PlayerConfiguration( configuration: PlayerConfiguration(
// 默认缓存 5M 大小 // 默认缓存 5M 大小
bufferSize: bufferSize:
videoType.value == 'live' ? 32 * 1024 * 1024 : 5 * 1024 * 1024, videoType == 'live' ? 32 * 1024 * 1024 : 5 * 1024 * 1024,
), ),
); );
@ -541,7 +541,7 @@ class PlPlayerController {
} }
/// 设置倍速 /// 设置倍速
if (videoType.value == 'live') { if (videoType == 'live') {
await setPlaybackSpeed(1.0); await setPlaybackSpeed(1.0);
} else { } else {
if (_playbackSpeed.value != 1.0) { if (_playbackSpeed.value != 1.0) {
@ -933,7 +933,7 @@ class PlPlayerController {
/// 设置长按倍速状态 live模式下禁用 /// 设置长按倍速状态 live模式下禁用
void setDoubleSpeedStatus(bool val) { void setDoubleSpeedStatus(bool val) {
if (videoType.value == 'live') { if (videoType == 'live') {
return; return;
} }
if (controlsLock.value) { if (controlsLock.value) {
@ -1015,7 +1015,7 @@ class PlPlayerController {
if (!_enableHeart) { if (!_enableHeart) {
return false; return false;
} }
if (videoType.value == 'live') { if (videoType == 'live') {
return; return;
} }
// 播放状态变化时,更新 // 播放状态变化时,更新
@ -1114,7 +1114,6 @@ class PlPlayerController {
// _buffered.close(); // _buffered.close();
// _showControls.close(); // _showControls.close();
// _controlsLock.close(); // _controlsLock.close();
// playerStatus.status.close(); // playerStatus.status.close();
// dataStatus.status.close(); // dataStatus.status.close();

View File

@ -652,7 +652,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
}, },
onDoubleTapDown: (TapDownDetails details) { onDoubleTapDown: (TapDownDetails details) {
// live模式下禁用 锁定时🔒禁用 // live模式下禁用 锁定时🔒禁用
if (_.videoType.value == 'live' || _.controlsLock.value) { if (_.videoType == 'live' || _.controlsLock.value) {
return; return;
} }
final double totalWidth = MediaQuery.sizeOf(context).width; final double totalWidth = MediaQuery.sizeOf(context).width;
@ -679,7 +679,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
/// 水平位置 快进 live模式下禁用 /// 水平位置 快进 live模式下禁用
onHorizontalDragUpdate: (DragUpdateDetails details) { onHorizontalDragUpdate: (DragUpdateDetails details) {
// live模式下禁用 锁定时🔒禁用 // live模式下禁用 锁定时🔒禁用
if (_.videoType.value == 'live' || _.controlsLock.value) { if (_.videoType == 'live' || _.controlsLock.value) {
return; return;
} }
// final double tapPosition = details.localPosition.dx; // final double tapPosition = details.localPosition.dx;
@ -695,7 +695,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
_.onChangedSliderStart(); _.onChangedSliderStart();
}, },
onHorizontalDragEnd: (DragEndDetails details) { onHorizontalDragEnd: (DragEndDetails details) {
if (_.videoType.value == 'live' || _.controlsLock.value) { if (_.videoType == 'live' || _.controlsLock.value) {
return; return;
} }
_.onChangedSliderEnd(); _.onChangedSliderEnd();
@ -826,7 +826,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
return const SizedBox(); return const SizedBox();
} }
if (_.videoType.value == 'live') { if (_.videoType == 'live') {
return const SizedBox(); return const SizedBox();
} }
if (value > max || max <= 0) { if (value > max || max <= 0) {
@ -879,7 +879,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
// 锁 // 锁
Obx( Obx(
() => Visibility( () => Visibility(
visible: _.videoType.value != 'live' && _.isFullScreen.value, visible: _.videoType != 'live' && _.isFullScreen.value,
child: Align( child: Align(
alignment: Alignment.centerLeft, alignment: Alignment.centerLeft,
child: FractionalTranslation( child: FractionalTranslation(

View File

@ -26,7 +26,7 @@ class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler {
static final List<MediaItem> _item = []; static final List<MediaItem> _item = [];
Box setting = GStrorage.setting; Box setting = GStrorage.setting;
bool enableBackgroundPlay = false; bool enableBackgroundPlay = false;
PlPlayerController player = PlPlayerController.getInstance(); PlPlayerController player = PlPlayerController();
VideoPlayerServiceHandler() { VideoPlayerServiceHandler() {
revalidateSetting(); revalidateSetting();

View File

@ -18,7 +18,7 @@ class AudioSessionHandler {
session.configure(const AudioSessionConfiguration.music()); session.configure(const AudioSessionConfiguration.music());
session.interruptionEventStream.listen((event) { session.interruptionEventStream.listen((event) {
final player = PlPlayerController.getInstance(videoType: 'none'); final player = PlPlayerController(videoType: 'none');
if (event.begin) { if (event.begin) {
if (!player.playerStatus.playing) return; if (!player.playerStatus.playing) return;
switch (event.type) { switch (event.type) {
@ -51,7 +51,7 @@ class AudioSessionHandler {
// 耳机拔出暂停 // 耳机拔出暂停
session.becomingNoisyEventStream.listen((_) { session.becomingNoisyEventStream.listen((_) {
final player = PlPlayerController.getInstance(videoType: 'none'); final player = PlPlayerController(videoType: 'none');
if (player.playerStatus.playing) { if (player.playerStatus.playing) {
player.pause(); player.pause();
} }

View File

@ -89,7 +89,7 @@ class ShutdownTimerService {
return; return;
} }
PlPlayerController plPlayerController = PlPlayerController plPlayerController =
PlPlayerController.getInstance(videoType: 'none'); PlPlayerController(videoType: 'none');
if (!exitApp && !waitForPlayingCompleted) { if (!exitApp && !waitForPlayingCompleted) {
if (!plPlayerController.playerStatus.playing) { if (!plPlayerController.playerStatus.playing) {
//仅提示用户 //仅提示用户
@ -124,7 +124,7 @@ class ShutdownTimerService {
} else { } else {
//暂停播放 //暂停播放
PlPlayerController plPlayerController = PlPlayerController plPlayerController =
PlPlayerController.getInstance(videoType: 'none'); PlPlayerController(videoType: 'none');
if (plPlayerController.playerStatus.playing) { if (plPlayerController.playerStatus.playing) {
plPlayerController.pause(); plPlayerController.pause();
waitForPlayingCompleted = true; waitForPlayingCompleted = true;