Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在AudioEngine的默认inputNode上安装tap并写入文件_Ios_Avfoundation_Avaudioengine - Fatal编程技术网

Ios 如何在AudioEngine的默认inputNode上安装tap并写入文件

Ios 如何在AudioEngine的默认inputNode上安装tap并写入文件,ios,avfoundation,avaudioengine,Ios,Avfoundation,Avaudioengine,我试图做一些非常简单的事情,但我只是断断续续地取得了进展。目前,我只想将麦克风中的音频数据保存到文件中。一旦我越过这个障碍,我将对块中的数据进行更多的处理。我已经厌倦了AVAudioEngine中的一些意识形态,我想知道我是否应该回到AudioUnits,它可能调试得更好一些(尽管更复杂) 首先,在实例化inputNode之前,AudioEngine似乎无法启动,然后您无法使用其outputFormatForBus获取其格式(采样率为零);您必须使用inputFormatForBus 现在我已经

我试图做一些非常简单的事情,但我只是断断续续地取得了进展。目前,我只想将麦克风中的音频数据保存到文件中。一旦我越过这个障碍,我将对块中的数据进行更多的处理。我已经厌倦了AVAudioEngine中的一些意识形态,我想知道我是否应该回到AudioUnits,它可能调试得更好一些(尽管更复杂)

首先,在实例化inputNode之前,AudioEngine似乎无法启动,然后您无法使用其outputFormatForBus获取其格式(采样率为零);您必须使用inputFormatForBus

现在我已经让它工作了,并且我收到了对块的调用,这些调用似乎是有效的数据,如果文件没有生成异常,我将无法写入该文件,错误如下:

将缓冲区数据写入文件时出错,操作无法完成。(com.apple.coreaudio.avfaudio错误1768846202)。”

(‘insz’) 这似乎表明我提供的格式或块获得的格式存在某种错误

有什么想法吗

_engine = [[AVAudioEngine alloc] init];

_outputFileURL = [NSURL URLWithString:[NSTemporaryDirectory() stringByAppendingString:@"tempOutput.caf"]];

AVAudioInputNode *inputNode = [_engine inputNode];
AVAudioFormat *format = [inputNode inputFormatForBus:1];
NSMutableDictionary *recordSettings = format.settings.mutableCopy;
[recordSettings addEntriesFromDictionary:@{
                              AVFormatIDKey : @(kAudioFormatMPEG4AAC),
                              AVEncoderAudioQualityKey : @(AVAudioQualityMedium)
                              }];

AVAudioFile *outputFile = [[AVAudioFile alloc] initForWriting:_outputFileURL settings:recordSettings error:&error];

[inputNode installTapOnBus:1 bufferSize:4096 format:format block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) {
    NSError *error;

    // as AVAudioPCMBuffer's are delivered this will write sequentially. The buffer's frameLength signifies how much of the buffer is to be written
    // IMPORTANT: The buffer format MUST match the file's processing format which is why outputFormatForBus: was used when creating the AVAudioFile object above
    NSAssert([outputFile writeFromBuffer:buffer error:&error], @"error writing buffer data to file, %@", [error localizedDescription]);
}];
if (!_engine.isRunning) [self startEngine];
多谢各位


根据@matt的评论更新了代码

以下是我如何解决这个问题的,以防其他人遇到同样的问题

@matt正确地评论说,我应该创建独立设置,而不是使用与inputNode关联的格式

commonFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32 sampleRate:44100 channels:2 interleaved:NO];

engine = [[AVAudioEngine alloc] init];
AVAudioInputNode *inputNode = engine.inputNode;

NSError *error;
AVAudioFile *outputFile = [[AVAudioFile alloc] initForWriting:_outputFileURL settings:commonFormat.settings error:&error];

[inputNode installTapOnBus:0 bufferSize:4096 format:commonFormat block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) {
    NSError *error;

    NSAssert([outputFile writeFromBuffer:buffer error:&error], @"error writing buffer data to file, %@", [error localizedDescription]);
}];

设置:[格式设置]
。这可能就是问题所在。您需要正确的输出文件格式设置。我只是尝试将数据作为PCM写入,以保持简单(请参阅Apple的使用VaudioEngine进行回放混音和录制示例)。我添加了字典键以将文件保存为MP4 AAC,但仍然没有成功。好的,但我仍然建议您根本不要将输出文件设置基于输入节点格式。尝试找到一组完全独立的工作设置。这就是我第一次写水龙头时必须做的。