在iOS7及以上版本中,在Receiver和Speaker之间切换音频输出?

时间:2014-10-01 12:17:19

标签: ios ios7 avaudiosession

我有一个音频播放器,当接近传感器通知时,可选择将音频输出从扬声器切换到接收器/听筒(无论是否连接了耳机)。以下是我的代码。

- (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 receiverenter link description here。我注意到这只会强制音频到扬声器,但不能确保路由单独进入接收器。此外,当转移到扬声器时,我收到以下错误:

  

AVAudioSession error overrideOutputAudioPort:Error Domain = NSOSStatusErrorDomain Code = -50“无法完成操作。(OSStatus error -50。)”

1 个答案:

答案 0 :(得分:7)

我通过避免覆盖

的覆盖来找出答案
- (void) setAudioSession:(NSString*)audioOutput{

        NSError* error;
        if([audioOutput isEqualToString:audioOutputSpeaker.lowercaseString]){

            //set the audioSession override
            if(![self setCategory:AVAudioSessionCategoryPlayAndRecord
                              withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionAllowBluetooth
                                    error:&error])
                NSLog(@"AVAudioSession error AVAudioSessionCategoryPlayAndRecord:%@",error);

            //activate the audio session
            if (![self setActive:YES error:&error])
                NSLog(@"AVAudioSession error activating: %@",error);
            else
                NSLog(@"AVAudioSession active with override: AVAudioSessionPortOverrideNone");
        }else if ([audioOutput isEqualToString:audioOutputReciever.lowercaseString]){
            //Force current audio out through reciever
            //set the audioSession override
            if(![self setCategory:AVAudioSessionCategoryPlayAndRecord
                              withOptions:AVAudioSessionCategoryOptionAllowBluetooth
                                    error:&error])
                NSLog(@"AVAudioSession error AVAudioSessionCategoryPlayAndRecord:%@",error);

            if (![self overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error])
                NSLog(@"AVAudioSession error overrideOutputAudioPort to Reciever:%@",error);

            //activate the audio session
            if (![self setActive:YES error:&error])
                NSLog(@"AVAudioSession error activating: %@",error);
            else
                NSLog(@"AVAudioSession active with override: AVAudioSessionPortOverrideNone");
        }
    }