feat: 支持不开启后台播放时的音频打断 支持有通知是减小音量通知结束时回复
This commit is contained in:
@ -646,6 +646,7 @@ class PlPlayerController {
|
||||
|
||||
playerStatus.status.value = PlayerStatus.playing;
|
||||
// screenManager.setOverlays(false);
|
||||
audioSessionHandler.setActive(true);
|
||||
}
|
||||
|
||||
/// 暂停播放
|
||||
|
@ -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<VideoPlayerServiceHandler> initAudioService() async {
|
||||
return await AudioService.init(
|
||||
@ -27,49 +26,9 @@ class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler {
|
||||
static final List<MediaItem> _item = [];
|
||||
Box setting = GStrorage.setting;
|
||||
bool enableBackgroundPlay = false;
|
||||
late AudioSession session;
|
||||
bool _playInterrupted = false; // 暂时无用
|
||||
|
||||
VideoPlayerServiceHandler() {
|
||||
revalidateSetting();
|
||||
initSession();
|
||||
}
|
||||
|
||||
Future<void> 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) {
|
||||
|
53
lib/services/audio_session.dart
Normal file
53
lib/services/audio_session.dart
Normal file
@ -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<void> 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();
|
||||
});
|
||||
}
|
||||
}
|
@ -1,8 +1,11 @@
|
||||
import 'audio_handler.dart';
|
||||
import 'audio_session.dart';
|
||||
|
||||
late VideoPlayerServiceHandler videoPlayerServiceHandler;
|
||||
late AudioSessionHandler audioSessionHandler;
|
||||
|
||||
Future<void> setupServiceLocator() async {
|
||||
final audio = await initAudioService();
|
||||
videoPlayerServiceHandler = audio;
|
||||
audioSessionHandler = AudioSessionHandler();
|
||||
}
|
||||
|
Reference in New Issue
Block a user