我正在尝试将音频重定向到
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
只听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; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。