0

我正在尝试kAudioUnitSubType_RemoteIO在没有麦克风的 Mac 上使用 Catalyst 上的单元。

问题是,总线 1(即输入)的整个初始化都很好,包括设置格式,甚至启用总线,但随后它失败了,出现了一些甚至没有在osstatus.comAudioOutputUnitStart(unit)上列出的模糊错误。-66628

但是,如果我仅使用输出总线 0 启动设备,则它运行良好并播放我的音频。

那么,有没有一种简单的方法可以在没有枚举所有设备的情况下检测 RemoteIO 后面没有设备的情况?只需尝试一些肯定会失败的功能(设置或获取参数?)。

这是我的代码:

    // Instantiate the unit
    var desc = AudioComponentDescription(componentType: kAudioUnitType_Output, componentSubType: kAudioUnitSubType_RemoteIO, componentManufacturer: kAudioUnitManufacturer_Apple, componentFlags: 0, componentFlagsMask: 0)
    let comp = AudioComponentFindNext(nil, &desc)!
    var unit: AudioUnit!
    NotError(AudioComponentInstanceNew(comp, &unit), 51000)

    // Set maximum buffer size as recommended by Apple
    var maxFramesPerSlice = UInt32(Self.maxFramesPerSlice)
    NotError(AudioUnitSetProperty(unit, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, &maxFramesPerSlice, SizeOf(maxFramesPerSlice)), 51003)

    // Set format and sample rate to the same as hardware output
    var descr = AudioStreamBasicDescription.canonical(isStereo: true, sampleRate: hardwareSampleRate)
    NotError(AudioUnitSetProperty(unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &descr, SizeOf(descr)), 51022)

    // Set render callback
    var callback = AURenderCallbackStruct(inputProc: stereoInputRenderCallback, inputProcRefCon: Bridge(obj: self))
    NotError(AudioUnitSetProperty(unit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Output, 1, &callback, SizeOf(callback)), 51008)

    // Enable input - required as it's disabled by default
    var enable: UInt32 = 1
    NotError(AudioUnitSetProperty(unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enable, SizeOf(enable)), 51021)

    NotError(AudioUnitInitialize(unit), 51007)

所以它通过了所有这些,并且只在OSStatus(-66628)这里失败:

    NotError(AudioOutputUnitStart(unit), 51009)

我认为在尝试启动设备之前必须有某种方法来检测设备的缺失。

编辑:理想情况下,我正在寻找一种跨平台的方式(ios/macOS)

4

1 回答 1

1

我相信检查AudioStreamBasicDescription返回的是否kAudioDevicePropertyStreamConfiguration, kAudioObjectPropertyScopeInputmBuffers > 0将表明设备是否存在。

于 2020-06-26T18:57:22.083 回答