在iOS7及以上版本中,在Receiver和Speaker之间切换音频输出? [英] Switching audio output between Receiver and Speaker in iOS7 and above?

查看:637
本文介绍了在iOS7及以上版本中,在Receiver和Speaker之间切换音频输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an audio player which has an option of switching the Audio Output from Speaker to Receiver/Earpiece (irrespective of whether headset is connected) when proximity sensor notifies 1. The following is my code for doing so.

- (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");

    }
 }

这是基于答案< a href =https://stackoverflow.com/questions/25042249/toggle-button-to-route-audio-to-receiver-or-speaker-in-ios-7>切换按钮将音频路由到扬声器和接收器和在此输入链接说明。我注意到这只会强制音频到扬声器,但不能确保路由单独进入接收器。此外,当转移到扬声器时,我收到以下错误:

This was based on the answer Toggle Button route audio to speaker and receiver and enter link description here. I noticed that this only forces the Audio to Speaker alone but does not ensure that the route goes to receiver alone. Moreover while shifting to speaker I get the following error:


AVAudioSession错误overrideOutputAudioPort:错误域= NSOSStatusErrorDomain代码= -50操作无法'完成。(OSStatus错误-50。)

AVAudioSession error overrideOutputAudioPort:Error Domain=NSOSStatusErrorDomain Code=-50 "The operation couldn’t be completed. (OSStatus error -50.)"


推荐答案

我找到了答案避免覆盖者的覆盖

I figured out the answer by avoiding overrides for Receiver

- (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");
        }
    }

这篇关于在iOS7及以上版本中,在Receiver和Speaker之间切换音频输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆