如何使用AVFoundation在音频输入源(蓝牙,内置麦克风)之间切换 [英] How to switch between audio input source(Bluetooth, BuiltIn Microphone) using AVFoundation

查看:100
本文介绍了如何使用AVFoundation在音频输入源(蓝牙,内置麦克风)之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前无法在BuiltInMicrophone和蓝牙麦克风iOS8之间切换音频输入源

I am currently having trouble with audio input source switching between builtInMicrophone and bluetooth microphone, iOS8

我试图找到在线解决方案,但一无所获:(

I tried to find online solutions, but got nothing :(

任何人,请给我建议正确的实现方式.

Anyone, please advice me proper way to achieve.

期待您的帮助!

推荐答案

我已经有了这段代码.

bluetoothInput只是在蓝牙麦克风和普通麦克风之间切换的布尔值.

bluetoothInput is just a boolean to switch between bluetooth mic and normal mic.

-(void) changeBluetoothInput{


if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
    if(self.bluetoothInput){
        //[[AVAudioSession sharedInstance] setActive:NO error:nil];

        [[AVAudioSession sharedInstance] setActive:YES error:nil];
        AVAudioSessionPortDescription* _bluetoothPort = [self bluetoothAudioDevice];
        [[AVAudioSession sharedInstance] setPreferredInput:_bluetoothPort
                                                     error:nil];
    }else{
        //[[AVAudioSession sharedInstance] setActive:NO error:nil];
        //[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
        [[AVAudioSession sharedInstance] setActive:YES error:nil];
        AVAudioSessionPortDescription* _bluetoothPort = [self normalAudioDevice];
        [[AVAudioSession sharedInstance] setPreferredInput:_bluetoothPort
                                                     error:nil];
    }
}

}

- (AVAudioSessionPortDescription*)bluetoothAudioDevice
{
    NSArray* bluetoothRoutes = @[AVAudioSessionPortBluetoothA2DP, AVAudioSessionPortBluetoothLE, AVAudioSessionPortBluetoothHFP];
    return [self audioDeviceFromTypes:bluetoothRoutes];
}

- (AVAudioSessionPortDescription*)normalAudioDevice
{
    NSArray* bluetoothRoutes = @[AVAudioSessionPortBuiltInMic];
    return [self audioDeviceFromTypes:bluetoothRoutes];
}


- (AVAudioSessionPortDescription*)audioDeviceFromTypes:(NSArray*)types
{
    NSArray* routes = [[AVAudioSession sharedInstance] availableInputs];
    for (AVAudioSessionPortDescription* route in routes)
    {
        if ([types containsObject:route.portType])
        {
            return route;
        }
    }
    return nil;
}

这篇关于如何使用AVFoundation在音频输入源(蓝牙,内置麦克风)之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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