From 2348c140084600cbc2bf909027f6736f5d89760b Mon Sep 17 00:00:00 2001 From: Riri Date: Sat, 28 Oct 2023 14:52:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E4=B8=8D=E5=BC=80?= =?UTF-8?q?=E5=90=AF=E5=90=8E=E5=8F=B0=E6=92=AD=E6=94=BE=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E9=9F=B3=E9=A2=91=E6=89=93=E6=96=AD=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=9C=89=E9=80=9A=E7=9F=A5=E6=98=AF=E5=87=8F=E5=B0=8F=E9=9F=B3?= =?UTF-8?q?=E9=87=8F=E9=80=9A=E7=9F=A5=E7=BB=93=E6=9D=9F=E6=97=B6=E5=9B=9E?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/plugin/pl_player/controller.dart | 1 + lib/services/audio_handler.dart | 47 ------------------------ lib/services/audio_session.dart | 53 ++++++++++++++++++++++++++++ lib/services/service_locator.dart | 3 ++ 4 files changed, 57 insertions(+), 47 deletions(-) create mode 100644 lib/services/audio_session.dart diff --git a/lib/plugin/pl_player/controller.dart b/lib/plugin/pl_player/controller.dart index e4f1b3d3..ec862738 100644 --- a/lib/plugin/pl_player/controller.dart +++ b/lib/plugin/pl_player/controller.dart @@ -646,6 +646,7 @@ class PlPlayerController { playerStatus.status.value = PlayerStatus.playing; // screenManager.setOverlays(false); + audioSessionHandler.setActive(true); } /// 暂停播放 diff --git a/lib/services/audio_handler.dart b/lib/services/audio_handler.dart index 67b8cb89..61b32b96 100644 --- a/lib/services/audio_handler.dart +++ b/lib/services/audio_handler.dart @@ -5,7 +5,6 @@ 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'; -import 'package:audio_session/audio_session.dart'; Future initAudioService() async { return await AudioService.init( @@ -27,49 +26,9 @@ class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler { static final List _item = []; Box setting = GStrorage.setting; bool enableBackgroundPlay = false; - late AudioSession session; - bool _playInterrupted = false; // 暂时无用 VideoPlayerServiceHandler() { revalidateSetting(); - initSession(); - } - - Future initSession() async { - session = await AudioSession.instance; - session.configure(const AudioSessionConfiguration.speech()); - - session.interruptionEventStream.listen((event) { - if (event.begin) { - switch (event.type) { - case AudioInterruptionType.duck: - // duck or pause - // break; - case AudioInterruptionType.pause: - case AudioInterruptionType.unknown: - pause(); - _playInterrupted = true; - break; - } - } else { - switch (event.type) { - case AudioInterruptionType.duck: - // unduck - // break; - case AudioInterruptionType.pause: - if (_playInterrupted) play(); - break; - case AudioInterruptionType.unknown: - break; - } - _playInterrupted = false; - } - }); - - // 耳机拔出暂停 - session.becomingNoisyEventStream.listen((_) { - pause(); - }); } revalidateSetting() { @@ -105,12 +64,6 @@ class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler { final AudioProcessingState processingState; final playing = status == PlayerStatus.playing; - if (playing) { - _playInterrupted = false; - session.setActive(true); - } else { - session.setActive(false); - } if (status == PlayerStatus.completed) { processingState = AudioProcessingState.completed; } else if (isBuffering) { diff --git a/lib/services/audio_session.dart b/lib/services/audio_session.dart new file mode 100644 index 00000000..3dd9db45 --- /dev/null +++ b/lib/services/audio_session.dart @@ -0,0 +1,53 @@ +import 'package:audio_session/audio_session.dart'; +import 'package:pilipala/plugin/pl_player/index.dart'; + +class AudioSessionHandler { + late AudioSession session; + bool _playInterrupted = false; + + setActive(bool active) { + session.setActive(active); + } + + AudioSessionHandler() { + initSession(); + } + + Future initSession() async { + session = await AudioSession.instance; + session.configure(const AudioSessionConfiguration.music()); + + session.interruptionEventStream.listen((event) { + final player = PlPlayerController.getInstance(); + if (event.begin) { + switch (event.type) { + case AudioInterruptionType.duck: + player.setVolume(player.volume.value * 0.5); + break; + case AudioInterruptionType.pause: + case AudioInterruptionType.unknown: + player.pause(); + _playInterrupted = true; + break; + } + } else { + switch (event.type) { + case AudioInterruptionType.duck: + player.setVolume(player.volume.value * 2); + break; + case AudioInterruptionType.pause: + if (_playInterrupted) PlPlayerController.getInstance().play(); + break; + case AudioInterruptionType.unknown: + break; + } + _playInterrupted = false; + } + }); + + // 耳机拔出暂停 + session.becomingNoisyEventStream.listen((_) { + PlPlayerController.getInstance().pause(); + }); + } +} diff --git a/lib/services/service_locator.dart b/lib/services/service_locator.dart index 8b9e5af0..e4497660 100644 --- a/lib/services/service_locator.dart +++ b/lib/services/service_locator.dart @@ -1,8 +1,11 @@ import 'audio_handler.dart'; +import 'audio_session.dart'; late VideoPlayerServiceHandler videoPlayerServiceHandler; +late AudioSessionHandler audioSessionHandler; Future setupServiceLocator() async { final audio = await initAudioService(); videoPlayerServiceHandler = audio; + audioSessionHandler = AudioSessionHandler(); }