How to route to kAudioSessionProperty_OverrideCategoryEnableBluetoothInput without using AudioSessionSetProperty

柔情痞子 提交于 2019-12-04 13:42:39
foundry

To expand on my previous answer and comment:

You would use the AVAudioSession method

- (BOOL)setCategory:(NSString *)category 
        withOptions:(AVAudioSessionCategoryOptions)options 
              error:(NSError **)outError 

with category as
AVAudioSessionCategoryPlayAndRecord
or AVAudioSessionCategoryRecord

and options as
AVAudioSessionCategoryOptionAllowBluetooth

In your reply you say

that is not the same because that would allow A2DP bluetooth only

But according to the Apple docs

AVAudioSessionCategoryOptionAllowBluetooth
Allows Bluetooth handsfree devices to appear as available input routes.

I understand that to mean bluetooth HFP, which I presume is what you are after. As regards "forcing", Apple is not too keen on apps forcing/overriding OS control of a user's experience of device behaviour.

It may be that this does not work in practice - I have not been able to test it. Presumably you have, and it fails (you don't indicate in your question). But you are hitting the limits of Apple's documentation on this issue. If you really can't get it to work I would be inclined to go with the deprecated C interface, and be prepared to make changes for iOS8.

Lewis Gordon

By referring to this answer, I came up with the following:

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&error];
    NSArray* routes = [audioSession availableInputs];
    for (AVAudioSessionPortDescription* route in routes)
    {
        if (route.portType == AVAudioSessionPortBluetoothHFP)
        {
            [audioSession setPreferredInput:route error:nil];
        }
    }

It appears to work the same way as the old property override and redirects both input and output the the hands free device.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!