From 1c3f8beeb1fad1080525a16f265bdbf30c62dbdd Mon Sep 17 00:00:00 2001 From: Riri Date: Thu, 26 Oct 2023 10:57:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=B3=E9=97=AD=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E6=92=AD=E6=94=BE=E6=97=B6=E4=B8=8D=E6=98=BE=E7=A4=BA=E5=AA=92?= =?UTF-8?q?=E4=BD=93=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pages/setting/play_setting.dart | 9 +++++++++ lib/services/audio_handler.dart | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/pages/setting/play_setting.dart b/lib/pages/setting/play_setting.dart index 82459be9..6dbfefcf 100644 --- a/lib/pages/setting/play_setting.dart +++ b/lib/pages/setting/play_setting.dart @@ -3,6 +3,7 @@ import 'package:get/get.dart'; import 'package:hive/hive.dart'; import 'package:pilipala/models/video/play/quality.dart'; import 'package:pilipala/plugin/pl_player/index.dart'; +import 'package:pilipala/services/service_locator.dart'; import 'package:pilipala/utils/storage.dart'; import 'widgets/switch_item.dart'; @@ -37,6 +38,14 @@ class _PlaySettingState extends State { defaultValue: BtmProgresBehavior.values.first.code); } + @override + void dispose() { + super.dispose(); + + // 重新验证媒体通知后台播放设置 + videoPlayerServiceHandler.revalidateSetting(); + } + @override Widget build(BuildContext context) { TextStyle titleStyle = Theme.of(context).textTheme.titleMedium!; diff --git a/lib/services/audio_handler.dart b/lib/services/audio_handler.dart index f9a622b5..a190a09e 100644 --- a/lib/services/audio_handler.dart +++ b/lib/services/audio_handler.dart @@ -1,8 +1,10 @@ import 'package:audio_service/audio_service.dart'; +import 'package:hive/hive.dart'; import 'package:pilipala/models/bangumi/info.dart'; import 'package:pilipala/models/video_detail_res.dart'; import 'package:get/get.dart'; import 'package:pilipala/plugin/pl_player/index.dart'; +import 'package:pilipala/utils/storage.dart'; Future initAudioService() async { return await AudioService.init( @@ -21,6 +23,17 @@ Future initAudioService() async { class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler { static final List _item = []; + Box setting = GStrorage.setting; + bool enableBackgroundPlay = false; + + VideoPlayerServiceHandler() { + revalidateSetting(); + } + + revalidateSetting() { + enableBackgroundPlay = + setting.get(SettingBoxKey.enableBackgroundPlay, defaultValue: false); + } @override Future play() async { @@ -41,10 +54,13 @@ class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler { } Future setMediaItem(MediaItem newMediaItem) async { + if (!enableBackgroundPlay) return; mediaItem.add(newMediaItem); } Future setPlaybackState(PlayerStatus status, bool isBuffering) async { + if (!enableBackgroundPlay) return; + final AudioProcessingState processingState; final playing = status == PlayerStatus.playing; if (status == PlayerStatus.completed) { @@ -72,11 +88,15 @@ class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler { } onStatusChange(PlayerStatus status, bool isBuffering) { + if (!enableBackgroundPlay) return; + if (_item.isEmpty) return; setPlaybackState(status, isBuffering); } onVideoDetailChange(dynamic data, int cid) { + if (!enableBackgroundPlay) return; + if (data == null) return; Map argMap = Get.arguments; final heroTag = argMap['heroTag']; @@ -119,6 +139,8 @@ class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler { } onVideoDetailDispose() { + if (!enableBackgroundPlay) return; + playbackState.add(playbackState.value.copyWith( processingState: AudioProcessingState.idle, playing: false, @@ -135,6 +157,8 @@ class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler { } clear() { + if (!enableBackgroundPlay) return; + mediaItem.add(null); playbackState.add(PlaybackState( processingState: AudioProcessingState.idle, @@ -145,6 +169,8 @@ class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler { } onPositionChange(Duration position) { + if (!enableBackgroundPlay) return; + playbackState.add(playbackState.value.copyWith( updatePosition: position, ));