【问题标题】:AVAudioRecorder/AVAudioSession with Apple AirpodsAVAudioRecorder/AVAudioSession 与 Apple Airpods
【发布时间】:2017-10-06 18:54:42
【问题描述】:

我在这里看到了已经问过的问题:

AirPods not working as an input source for Voice Recorder App

我已经签入了这个帖子,但没有任何回应。

但是,有谁知道 AVAudioRecorder 是否/为什么无法将 AirPods 用作在应用程序中录制音频的输入设备?我通过内置麦克风以及其他 BT 设备(Beats、cheapo BT 扬声器电话等)进行录音,但是在使用 AirPods 时我无法捕获音频。

此外,当即将录制时,我正在循环使用可用的输入并强制输入为 BT 设备(参见下面的代码),在本例中为 AirPods。同样,适用于除 AirPods 之外的所有其他 BT 设备。

想法?关于我们在这里做错了什么的任何指导都会很棒。这太让人抓狂了。

NSError *error;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord 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)
    }
}

【问题讨论】:

    标签: ios objective-c bluetooth avaudiosession


    【解决方案1】:

    原来我们遇到的问题与正在设置的类别有关。由于我们在使用各种蓝牙输出设备时遇到问题,我们将音频类别设置为 AVAudioSessionCategoryPlayback,除非我们准备好录制。

    根据此 Stack 帖子:AVAudioSession: Some Bluetooth devices are not working properly on my App

    在上面的代码中,我们在录制之前将类别切换到AVAudioSessionCategoryRecord。虽然这适用于内置麦克风和其他蓝牙设备,但它不适用于 AirPods。相反,将类别设置为 AVAudioSessionCategoryPlayAndRecord 可以让 AirPods 进行录制。

    然后,我仍然为整个应用程序中的会话维护一个仅播放类别以进行音频播放。仅在即将录制音频时切换到 PlayAndRecord。

    附带说明:Apple 并未将 AirPods 列为 MFi 设备。 https://mfi.apple.com/MFiWeb/getFAQ.action#1-1

    【讨论】:

    • 我自己也遇到了这个问题,从 .record 切换到 .playAndRecord 确实解决了我的 AirPods 问题。你知道为什么吗?我测试过的所有其他蓝牙耳机(超过 10 多个耳机)都可以与 .record 一起使用。我很高兴修复如此简单,但担心根本原因没有得到解决。
    【解决方案2】:

    我认为 AirPods 是 MFI(Made For Iphone)配件,这意味着蓝牙通信通过 ExternalAccessory 框架https://developer.apple.com/documentation/externalaccessory

    这是苹果演示: https://developer.apple.com/library/content/samplecode/EADemo/Introduction/Intro.html

    提示:协议名称必须放在 UISupportedExternalAccessoryProtocols 键中的 Info.plist 中

    更多详情:https://mfi.apple.com/MFiWeb/getFAQ.action

    【讨论】:

    • 谢谢。我会看看这个,看看它是否有帮助。
    • 我不会对此投反对票,但 Airpods 未列为 MFI,因此 ExternalAccessory 无法工作,但很好! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 2023-03-10
    • 2018-05-01
    相关资源
    最近更新 更多