gpt4 book ai didi

ios - 如何阻止 Avaudiosesion 停用?

转载 作者:行者123 更新时间:2023-11-29 01:00:02 27 4
gpt4 key购买 nike

我想通过扬声器播放歌曲,同时能够使用 Quickblox 接听视频通话。

我的音频速率越来越乱了。还有一个更大的问题是,当通话结束时,quickblox 框架将 Audio Session 设置为停用状态。 IE [Avaudiosession sharedInstance]setActive:NO....

如何阻止这种情况发生?

或者有没有办法处理上述情况。??

我已经通过谷歌阅读了一个月,但仍然没有找到任何合适的答案或任何指南。谁能帮我解决这个问题/??

最佳答案

首先,要允许 AVAudioSession 与其他 AVAudioSession 一起工作,您需要在初始化时设置选项 AVAudioSessionCategoryOptionMixWithOthers:

NSError *sessionError = NULL;
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:&sessionError];

if(!success) {
NSLog(@"Error setting category Audio Session: %@", [sessionError localizedDescription]);
}

要处理中断(电话、闹钟等),您应该为 NSNotificationCenter 的中断设置一个观察者,您将能够处理 的激活/停用code>AVAudioSession 如有必要:

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleAudioSessionInterruption:)
name:AVAudioSessionInterruptionNotification
object:[AVAudioSession sharedInstance]];

NSNotification 会携带 Type of Interruption 和 Key:

- (void)handleAudioSessionInterruption:(NSNotification*)notification {

NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];

switch (interruptionType.unsignedIntegerValue) {
case AVAudioSessionInterruptionTypeBegan:{
// • Audio has stopped, already inactive
// • Change state of UI, etc., to reflect non-playing state
NSLog(@"AVAudioSessionInterruptionTypeBegan");

} break;
case AVAudioSessionInterruptionTypeEnded:{
// • Make session active
// • Update user interface
// • AVAudioSessionInterruptionOptionShouldResume option
NSLog(@"AVAudioSessionInterruptionTypeEnded");
if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
// Here you should continue playback.
}
} break;
default:
break;
}
}

关于ios - 如何阻止 Avaudiosesion 停用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37123598/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com