|
@@ -85,13 +85,16 @@ NSString * const DFPlaybackLikelyToKeepUpKey = @"playbackLikelyToKeepUp";
|
|
|
- (void)df_initPlayerWithUserId:(NSString *)userId{
|
|
|
[DFPlayerFileManager df_saveUserId:userId];
|
|
|
|
|
|
- [[AVAudioSession sharedInstance] setActive:YES error:nil];
|
|
|
//[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
|
|
|
//hxd change 20240723
|
|
|
- [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
|
|
|
+ //[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryMultiRoute error:nil];
|
|
|
+ [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
|
|
|
+ [[AVAudioSession sharedInstance] setActive:YES error:nil];
|
|
|
+ //[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers error:nil];
|
|
|
+ //[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback mode:(nonnull AVAudioSessionMode) options:<#(AVAudioSessionCategoryOptions)#> error:<#(NSError *__autoreleasing _Nullable * _Nullable)#>
|
|
|
|
|
|
_isOtherPlaying = [AVAudioSession sharedInstance].otherAudioPlaying;
|
|
|
-
|
|
|
+
|
|
|
self.playMode = DFPlayerModeOnlyOnce;
|
|
|
self.state = DFPlayerStateStopped;
|
|
|
self.isObserveProgress = YES;
|
|
@@ -150,10 +153,22 @@ NSString * const DFPlaybackLikelyToKeepUpKey = @"playbackLikelyToKeepUp";
|
|
|
name:AVAudioSessionInterruptionNotification
|
|
|
object:[AVAudioSession sharedInstance]];
|
|
|
//监测其他app是否占据AudioSession
|
|
|
+// [notificationCenter addObserver:self
|
|
|
+// selector:@selector(df_playerSecondaryAudioHint:)
|
|
|
+// name:AVAudioSessionSilenceSecondaryAudioHintNotification
|
|
|
+// object:nil];
|
|
|
+
|
|
|
[notificationCenter addObserver:self
|
|
|
- selector:@selector(df_playerSecondaryAudioHint:)
|
|
|
+ selector:@selector(handleAudioSessionSilenceSecondaryAudioHintNotification:)
|
|
|
name:AVAudioSessionSilenceSecondaryAudioHintNotification
|
|
|
object:nil];
|
|
|
+
|
|
|
+ // 监听中断通知
|
|
|
+ [[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
+ selector:@selector(handleAudioSessionInterruption:)
|
|
|
+ name:AVAudioSessionInterruptionNotification
|
|
|
+ object:[AVAudioSession sharedInstance]];
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- (void)df_playerWillResignActive{
|
|
@@ -885,6 +900,60 @@ NSString * const DFPlaybackLikelyToKeepUpKey = @"playbackLikelyToKeepUp";
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+#pragma mark - hxd add 20240725 音频播放呗中断检测
|
|
|
+- (void)handleAudioSessionInterruption:(NSNotification *)notification {
|
|
|
+ if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
|
|
|
+ AVAudioSessionInterruptionType type = [[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] integerValue];
|
|
|
+
|
|
|
+ switch (type) {
|
|
|
+ case AVAudioSessionInterruptionTypeBegan:
|
|
|
+ // 播放被中断,此处可以暂停播放
|
|
|
+ HLog(@"Audio Session Interruption Began");
|
|
|
+ // 暂停你的音频播放
|
|
|
+ [self df_pause];
|
|
|
+ break;
|
|
|
+
|
|
|
+ case AVAudioSessionInterruptionTypeEnded:
|
|
|
+ // 播放中断结束,检查是否应该恢复播放
|
|
|
+ HLog(@"Audio Session Interruption Ended");
|
|
|
+
|
|
|
+ // 获取中断选项,判断是否可以恢复播放
|
|
|
+ AVAudioSessionInterruptionOptions options = [[notification.userInfo valueForKey:AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
|
|
|
+
|
|
|
+ if (options == AVAudioSessionInterruptionOptionShouldResume) {
|
|
|
+ // 如果应该恢复播放,则恢复播放
|
|
|
+ [self df_play];
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+- (void)handleAudioSessionSilenceSecondaryAudioHintNotification:(NSNotification *)notification {
|
|
|
+ // 这里处理通知,比如暂停或恢复你的音频播放
|
|
|
+ //AVAudioSession *session = [notification object];
|
|
|
+ BOOL willSilenceOthers = [[notification.userInfo valueForKey:AVAudioSessionSilenceSecondaryAudioHintTypeKey] boolValue];
|
|
|
+
|
|
|
+ if (willSilenceOthers) {
|
|
|
+ // 其他音频将被暂停
|
|
|
+ HLog(@"Other audio will be silenced.");
|
|
|
+ // 这里可以添加代码来暂停你的音频播放(如果需要的话)
|
|
|
+ [self df_pause];
|
|
|
+ } else {
|
|
|
+ // 其他音频将恢复播放
|
|
|
+ HLog(@"Other audio will resume.");
|
|
|
+ // 这里可以添加代码来恢复你的音频播放(如果需要的话)
|
|
|
+ [self df_play];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
@end
|
|
|
|
|
|
|