From cd3385be63fdb7670217e9569eefe88da0db7310 Mon Sep 17 00:00:00 2001 From: bggRGjQaUbCoE Date: Tue, 25 Mar 2025 18:25:08 +0800 Subject: [PATCH] opt: only play audio Closes #519 Signed-off-by: bggRGjQaUbCoE --- .../video/detail/widgets/header_control.dart | 13 +++++++- lib/plugin/pl_player/controller.dart | 33 +++++++++---------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/pages/video/detail/widgets/header_control.dart b/lib/pages/video/detail/widgets/header_control.dart index a2247604..2876b7c5 100644 --- a/lib/pages/video/detail/widgets/header_control.dart +++ b/lib/pages/video/detail/widgets/header_control.dart @@ -318,7 +318,18 @@ class _HeaderControlState extends State { 2 => Obx( () => ActionRowLineItem( iconData: Icons.headphones, - onTap: widget.controller.setOnlyPlayAudio, + onTap: () { + widget.controller.onlyPlayAudio.value = + !widget.controller.onlyPlayAudio.value; + if (widget.controller.onlyPlayAudio.value) { + widget.videoDetailCtr.playerInit( + video: widget.videoDetailCtr.audioUrl ?? '', + audio: '', + ); + } else { + widget.videoDetailCtr.playerInit(); + } + }, text: " 听视频 ", selectStatus: widget.controller.onlyPlayAudio.value, ), diff --git a/lib/plugin/pl_player/controller.dart b/lib/plugin/pl_player/controller.dart index 0d108f7f..c73fb31a 100644 --- a/lib/plugin/pl_player/controller.dart +++ b/lib/plugin/pl_player/controller.dart @@ -90,8 +90,6 @@ class PlPlayerController { /// 后台播放 late final Rx _continuePlayInBackground = false.obs; - late final Rx _onlyPlayAudio = false.obs; - late final Rx _flipX = false.obs; late final Rx _flipY = false.obs; @@ -226,7 +224,7 @@ class PlPlayerController { Rx get continuePlayInBackground => _continuePlayInBackground; /// 听视频 - Rx get onlyPlayAudio => _onlyPlayAudio; + late final Rx onlyPlayAudio = false.obs; /// 镜像 Rx get flipX => _flipX; @@ -713,7 +711,7 @@ class PlPlayerController { AudioTrack.auto(), ); // 音轨 - if (dataSource.audioSource?.isNotEmpty ?? false) { + if (dataSource.audioSource?.isNotEmpty == true) { await pp.setProperty( 'audio-files', UniversalPlatform.isWindows @@ -721,10 +719,7 @@ class PlPlayerController { : dataSource.audioSource!.replaceAll(':', '\\:'), ); } else { - await pp.setProperty( - 'audio-files', - '', - ); + await pp.setProperty('audio-files', ''); } // 字幕 @@ -778,11 +773,11 @@ class PlPlayerController { SmartDialog.showToast('视频播放器为空,请重新进入本页面'); return false; } - if (dataSource.videoSource?.isEmpty ?? true) { + if (dataSource.videoSource.isNullOrEmpty) { SmartDialog.showToast('视频源为空,请重新进入本页面'); return false; } - if (dataSource.audioSource?.isEmpty ?? true) { + if (dataSource.audioSource.isNullOrEmpty) { SmartDialog.showToast('音频源为空'); } else { await (_videoPlayerController!.platform as NativePlayer).setProperty( @@ -947,12 +942,16 @@ class PlPlayerController { } else if (event.startsWith('Could not open codec')) { SmartDialog.showToast('无法加载解码器, $event,可能会切换至软解'); return; - } else if (event.startsWith("Failed to open .") || - event.startsWith("Cannot open file ''")) { - SmartDialog.showToast('视频源为空'); } else { - SmartDialog.showToast('视频加载错误, $event'); - debugPrint('视频加载错误, $event'); + if (onlyPlayAudio.value.not) { + if (event.startsWith("Failed to open .") || + event.startsWith("Cannot open file ''")) { + SmartDialog.showToast('视频源为空'); + } else { + SmartDialog.showToast('视频加载错误, $event'); + debugPrint('视频加载错误, $event'); + } + } } }), // videoPlayerController!.stream.volume.listen((event) { @@ -1609,9 +1608,9 @@ class PlPlayerController { } void setOnlyPlayAudio() { - _onlyPlayAudio.value = !_onlyPlayAudio.value; + onlyPlayAudio.value = !onlyPlayAudio.value; videoPlayerController?.setVideoTrack( - _onlyPlayAudio.value ? VideoTrack.no() : VideoTrack.auto()); + onlyPlayAudio.value ? VideoTrack.no() : VideoTrack.auto()); } late final showSeekPreview = GStorage.showSeekPreview;