gpt4 book ai didi

iphone - "shm_open failed"和来自 AudioUnitInitialize with RemoteIO 的错误 3

转载 作者:搜寻专家 更新时间:2023-10-30 20:10:04 26 4
gpt4 key购买 nike

我正在尝试使用以下代码从 Core Audio(远程 I/O 音频单元)获取麦克风输入:

- (void)audioAU
{
enum : AudioUnitElement {
kOutputElement = 0,
kInputElement = 1
};
const UInt32 disableFlag = 0;
const UInt32 enableFlag = 1;

OSStatus err = noErr;
NSError *error = nil;


// Configure & activate audio session

AVAudioSession *session = [AVAudioSession sharedInstance];

if (![session setCategory:AVAudioSessionCategoryRecord error:&error]) NSLog(@"Error configuring session category: %@", error);
if (![session setMode:AVAudioSessionModeMeasurement error:&error]) NSLog(@"Error configuring session mode: %@", error);
if (![session setActive:YES error:&error]) NSLog(@"Error activating audio session: %@", error);

NSLog(@"Session activated. sample rate %f", session.sampleRate);
NSLog(@"Number of channels %d", session.inputNumberOfChannels);


// Set up Remote I/O audio unit for audio capture

AudioComponent component = AudioComponentFindNext(NULL, &(const AudioComponentDescription){
.componentType = kAudioUnitType_Output,
.componentSubType = kAudioUnitSubType_RemoteIO,
.componentManufacturer = kAudioUnitManufacturer_Apple,
.componentFlags = 0,
.componentFlagsMask = 0
});

AudioComponentInstance unit;

// Create audio component
err = AudioComponentInstanceNew(component, &unit);
if (err != noErr) NSLog(@"Error instantiating audio unit: %ld", err);

// Enable input
err = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputElement, &enableFlag, sizeof(enableFlag));
if (err != noErr) NSLog(@"Error enabling input for audio unit: %ld", err);

AudioStreamBasicDescription streamDesc = {
.mSampleRate = session.sampleRate,
.mFormatID = kAudioFormatLinearPCM,
.mFormatFlags = kAudioFormatFlagsAudioUnitCanonical /*matches AudioUnitSampleType*/ | kAudioFormatFlagIsNonInterleaved,
.mBytesPerPacket = sizeof(AudioUnitSampleType),
.mFramesPerPacket = 1,
.mBytesPerFrame = sizeof(AudioUnitSampleType) * session.inputNumberOfChannels,
.mChannelsPerFrame = session.inputNumberOfChannels,
.mBitsPerChannel = 8 * sizeof(AudioUnitSampleType),
.mReserved = 0,
};
err = AudioUnitSetProperty(unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputElement, &streamDesc, sizeof(streamDesc));
if (err != noErr) NSLog(@"Error configuring input stream format for audio unit: %ld", err);

AURenderCallbackStruct callbacks = {
.inputProc = renderCallback,
.inputProcRefCon = unit
};
err = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Input, kOutputElement, &callbacks, sizeof(callbacks));
if (err != noErr) NSLog(@"Error configuring input callbacks for audio unit: %ld", err);

err = AudioUnitInitialize(unit);
if (err != noErr) NSLog(@"Error initializing audio unit: %ld", err);

err = AudioOutputUnitStart(unit);
if (err != noErr) NSLog(@"Error starting audio unit: %ld", err);

// err = AudioComponentInstanceDispose(unit);
// if (err != noErr) NSLog(@"Error disposing audio unit: %ld", err);
}

它在模拟器中运行(或至少不会崩溃),但在 iPhone 5 上运行时我得到了这个输出:

Session activated. sample rate 44100.000000
Number of channels 1
15:48:45.170 <com.apple.main-thread> shm_open failed: "AppleAURemoteIO.o.262da" (23) flags=0x2 errno=2
15:48:45.172 <com.apple.main-thread> AURemoteIO::ChangeHardwareFormats: error 3
15:48:45.333 <com.apple.main-thread> shm_open failed: "AppleAURemoteIO.o.262da" (23) flags=0x2 errno=2
15:48:45.336 <com.apple.main-thread> AURemoteIO::ChangeHardwareFormats: error 3
15:48:45.497 <com.apple.main-thread> shm_open failed: "AppleAURemoteIO.o.262da" (23) flags=0x2 errno=2
15:48:45.499 <com.apple.main-thread> AURemoteIO::ChangeHardwareFormats: error 3
15:48:45.669 <com.apple.main-thread> shm_open failed: "AppleAURemoteIO.o.262da" (23) flags=0x2 errno=2
15:48:45.671 <com.apple.main-thread> AURemoteIO::ChangeHardwareFormats: error 3
15:48:45.839 <com.apple.main-thread> shm_open failed: "AppleAURemoteIO.o.262da" (23) flags=0x2 errno=2
15:48:45.841 <com.apple.main-thread> AURemoteIO::ChangeHardwareFormats: error 3
Error initializing audio unit: 3

最佳答案

事实证明,当使用 AVAudioSessionCategoryRecord 时(或者等效地,来自 C API 的 kAudioSessionCategory_RecordAudio),您还必须禁用音频单元上的输出:

    ...
// Disable output
err = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputElement, &disableFlag, sizeof(disableFlag));
if (err != noErr) NSLog(@"Error disabling output for audio unit: %ld", err);

关于iphone - "shm_open failed"和来自 AudioUnitInitialize with RemoteIO 的错误 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14167958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com