Skip to content

Commit

Permalink
mod: add more setActive
Browse files Browse the repository at this point in the history
  • Loading branch information
orz12 committed Jul 21, 2024
1 parent 0404e74 commit e2ce6c3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
6 changes: 4 additions & 2 deletions lib/plugin/pl_player/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ class PlPlayerController {
if (!_listenersInitialized) {
startListeners();
}

audioSessionHandler.setActive(true);
await _initializePlayer(seekTo: seekTo);
if (videoType.value != 'live' && _cid != 0) {
refreshSubtitles().then((value) {
Expand Down Expand Up @@ -616,6 +618,7 @@ class PlPlayerController {
: dataSource.audioSource!.replaceAll(':', '\\:'),
);
}
audioSessionHandler.setActive(true);
await _videoPlayerController!.open(
Media(
dataSource.videoSource!,
Expand Down Expand Up @@ -882,7 +885,7 @@ class PlPlayerController {
/// TODO _duration.value丢失
Future<void> play({bool repeat = false, bool hideControls = true}) async {
if (_playerCount.value == 0) return;
audioSessionHandler.setActive(true);
await audioSessionHandler.setActive(true);
// 播放时自动隐藏控制条
controls = !hideControls;
// repeat为true,将从头播放
Expand All @@ -898,7 +901,6 @@ class PlPlayerController {

playerStatus.status.value = PlayerStatus.playing;
// screenManager.setOverlays(false);

}

/// 暂停播放
Expand Down
5 changes: 4 additions & 1 deletion lib/services/audio_handler.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:PiliPalaX/services/service_locator.dart';
import 'package:audio_service/audio_service.dart';
import 'package:flutter/cupertino.dart';
import 'package:hive/hive.dart';
Expand Down Expand Up @@ -64,7 +65,7 @@ class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler {
// print(newMediaItem);
// print(newMediaItem.title);
// debugPrint(StackTrace.current.toString());
if(!mediaItem.isClosed) mediaItem.add(newMediaItem);
if (!mediaItem.isClosed) mediaItem.add(newMediaItem);
}

Future<void> setPlaybackState(PlayerStatus status, bool isBuffering) async {
Expand All @@ -90,6 +91,7 @@ class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler {
MediaControl.fastForward
.copyWith(androidIcon: 'drawable/ic_baseline_forward_10_24'),
],
androidCompactActionIndices: const [0, 1, 2],
playing: playing,
systemActions: const {
MediaAction.seek,
Expand Down Expand Up @@ -146,6 +148,7 @@ class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler {
if (mediaItem == null) return;
// print("exist: ${PlPlayerController.instanceExists()}");
if (!PlPlayerController.instanceExists()) return;
audioSessionHandler.setActive(true);
_item.add(mediaItem);
setMediaItem(mediaItem);
}
Expand Down
14 changes: 1 addition & 13 deletions lib/services/audio_session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,8 @@ class AudioSessionHandler {
// player.setVolume(player.volume.value * 0.5);
break;
case AudioInterruptionType.pause:
PlPlayerController.pauseIfExists(isInterrupt: true);
// player.pause(isInterrupt: true);
_playInterrupted = true;
break;
case AudioInterruptionType.unknown:
PlPlayerController.pauseIfExists(isInterrupt: true);
// player.pause(isInterrupt: true);
_playInterrupted = true;
break;
}
Expand All @@ -49,15 +44,8 @@ class AudioSessionHandler {
// player.setVolume(player.volume.value * 2);
break;
case AudioInterruptionType.pause:
if (_playInterrupted && await setActive(true)) {
PlPlayerController.playIfExists();
} else {
SmartDialog.showToast(
'The request was denied and the app should not play audio');
}
//player.play();
break;
case AudioInterruptionType.unknown:
if (_playInterrupted) PlPlayerController.playIfExists();
break;
}
_playInterrupted = false;
Expand Down

0 comments on commit e2ce6c3

Please sign in to comment.