gpt4 book ai didi

ios - 如何在 AppRTC iOS 示例中将音频重定向到扬声器?

转载 作者:技术小花猫 更新时间:2023-10-29 11:16:15 24 4
gpt4 key购买 nike

我正在尝试将音频重定向到 AppRTC iOS example 中的扬声器.

我试过:

AVAudioSession* session = [AVAudioSession sharedInstance];

//error handling
BOOL success;
NSError* error;

//set the audioSession category.
//Needs to be Record or PlayAndRecord to use audioRouteOverride:

success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
error:&error];

if (!success) NSLog(@"AVAudioSession error setting category:%@",error);

//set the audioSession override
success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
error:&error];
if (!success) NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);

//activate the audio session
success = [session setActive:YES error:&error];
if (!success) NSLog(@"AVAudioSession error activating: %@",error);
else NSLog(@"audioSession active");

没有错误,但它不起作用。我该如何解决这个问题?

最佳答案

我通过解决方案解决了。只听 AVAudioSessionRouteChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSessionRouteChange:) name:AVAudioSessionRouteChangeNotification object:nil];

并使用 didSessionRouteChange 选择器如下:

- (void)didSessionRouteChange:(NSNotification *)notification
{
NSDictionary *interuptionDict = notification.userInfo;
NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];

switch (routeChangeReason) {
case AVAudioSessionRouteChangeReasonCategoryChange: {
// Set speaker as default route
NSError* error;
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
}
break;

default:
break;
}
}

关于ios - 如何在 AppRTC iOS 示例中将音频重定向到扬声器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24595579/

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