5
我有一個音頻播放器,當接近傳感器通知1時,可以選擇將音頻輸出從揚聲器切換到接收器/聽筒(不管耳機是否連接)。以下是我的代碼。在iOS7及更高版本的接收器和揚聲器之間切換音頻輸出?
- (void) switchAudioOutput:(NSString*)output{
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
BOOL success;
NSError* error;
if([output isEqualToString:keAudioOutputReciever]){
//Force current audio out through reciever
//set the audioSession override
success = [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone
error:&error];
if (!success)
NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);
//activate the audio session
success = [audioSession setActive:YES error:&error];
if (!success)
NSLog(@"AVAudioSession error activating: %@",error);
else
NSLog(@"AVAudioSession active with override: AVAudioSessionPortOverrideNone");
}else if([output isEqualToString:keAudioOutputSpeaker]){
//set the audioSession override
success = [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
error:&error];
if (!success)
NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);
//activate the audio session
success = [audioSession setActive:YES error:&error];
if (!success)
NSLog(@"AVAudioSession error activating: %@",error);
else
NSLog(@"AVAudioSession active with override: AVAudioSessionPortOverrideSpeaker");
}
}
這是基於答案Toggle Button route audio to speaker and receiver和enter link description here。我注意到這隻會強制單獨使用音頻至揚聲器,但不能確保路由僅通過接收器。此外,同時轉移到揚聲器我收到以下錯誤:
AVAudioSession error overrideOutputAudioPort:Error Domain=NSOSStatusErrorDomain Code=-50 "The operation couldn’t be completed. (OSStatus error -50.)"
我也歌廳完全相同的錯誤想出了答案。有人設法解決這個問題嗎? – 2015-01-26 08:50:33