微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当应用程序在后台时开始使用 MFi 设备录制

如何解决当应用程序在后台时开始使用 MFi 设备录制

我正在使用 AVAudioSession 在 MFi 配套应用程序上进行录音,但我只能在应用程序处于前台时进行录音,而不能在应用程序处于后台或手机屏幕锁定时进行。我正在使用 here 中的代码。唯一不同的是我将类别从 AVAudioSessionCategoryRecord 更改为 AVAudioSessionCategoryPlayAndRecord。我实际上都试过了,但没有运气:

NSError *error;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession AVAudioSessionCategoryPlayAndRecord withOptions:audioSession.categoryOptions|AVAudioSessionCategoryOptionAllowBluetooth
                    error:&error];
[audioSession setActive:YES error:nil];

NSLog(@"Data sources: %@",[audioSession availableInputs]);
// Data sources: ("<AVAudioSessionPortDescription: 0x1706071b0,type = MicrophoneBuiltIn; name = iPhone Microphone; UID = Built-In Microphone; selectedDataSource = Bottom>","<AVAudioSessionPortDescription: 0x170611bd0,type = BluetoothHFP; name = Dan\U2019s AirPods; UID = 50:32:37:E0:90:37-tsco; selectedDataSource = (null)>"    

for (AVAudioSessionPortDescription *desc in [audioSession availableInputs]){
    NSLog(@"Port desc: %@",desc.portType);
    // Loop: 1) Port desc: MicrophoneBuiltIn
    //       2) Port desc: BluetoothHFP

    if (desc.portType == AVAudioSessionPortBluetoothHFP) {
        NSLog(@"Trying to change preferred input");
        NSError *error;
        BOOL didSet = [audioSession setPreferredInput:desc error:&error];
        Nsstring *didSetString = didSet ? @"True" : @"False";
        NSLog(@"Post change preferred input: %@,error: %@",didSetString,error);
        // Post change preferred input: True,error: (null)
    }
}

我还确保将“音频”添加UIBackgroundModes

当应用程序在后台时,我可以看到从电话日志记录时打开了 HFP:

default 17:39:44.628437 -0800   bluetoothd  Bluetooth Daemon: HFP streaming to device 3C7FBB65: on
default 17:39:44.637577 -0800   bluetoothd  HFP stream started: Enabling diversity/TxBF

但是当应用程序在后台时,我只能在电话日志中看到这一点:

-[NSDictionary(xpcObject) _cs_xpcObject]_block_invoke Cannot encode non-plist types into xpc object : <AVAudioSessionRouteDescription: 0x105704410,inputs = (
    "<AVAudioSessionPortDescription: 0x105704b50,type = BluetoothHFP; name = DEVICE_NAME; UID = AC:63:BE:CF:CB:2C-tsco; selectedDataSource = (null)>"
); 
outputs = (
    "<AVAudioSessionPortDescription: 0x106005040,type = BluetoothHFP; name = DEVICE_NAME; UID = AC:63:BE:CF:CB:2C-tsco; selectedDataSource = (null)>"
)>

我假设在应用后台启动录制时未打开 HFP 通道。

我的理解是,无论是否连接BT设备,Apple/iOS都禁止应用在后台时启动录音。我的问题是,BT设备能不能自己打开HFP通道,这样当app调用AVAudioSession录音时,语音流会被AVAudioSession拾取?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。