簡體   English   中英

如何使用 AVFoundation 在音頻輸入源(藍牙、內置麥克風)之間切換

[英]How to switch between audio input source(Bluetooth, BuiltIn Microphone) using AVFoundation

我目前在內置麥克風和藍牙麥克風、iOS8 之間切換音頻輸入源時遇到問題

我試圖找到在線解決方案,但一無所獲:(

任何人,請建議我正確的方法來實現。

期待您的幫助!

我有這個代碼。

bluetoothInput 只是一個布爾值,用於在藍牙麥克風和普通麥克風之間切換。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM