RemoteIO录制的音频文件是静音或4KB

时间:2012-11-21 20:45:31

标签: ios audio-recording remoteio

我正在成功使用RemoteIO对来自麦克风的传入音频流进行分析。但是我似乎无法将文件写入磁盘。我已经阅读了很多问题:

并尝试在那里实施建议:除非他们不工作。哪里是调用ExtAudioFileWriteAsync的正确位置,我该如何设置它?

1 个答案:

答案 0 :(得分:3)

除了(相当艰巨但更好地涵盖Apple's example code)RemoteIO本身的设置过程之外,关键点是:

  1. 首先使用我用来设置流的AudioStreamBasicDescription*audioFormat)。根据其他问题和帖子,我不知道我花了多长时间尝试建立一个参数略有不同的新参数。只需从我的ivar引用流属性就足够了。
  2. 设置“isRecording”bool,以便您可以打开和关闭写入文件,而无需拆除和重新设置RemoteIO会话
  3. 可以写入recordingCallback,嗯,回调中的文件,但是异步执行。很多信息都谈到在playbackCallback中做这件事或设置第三个audioFileWriteCallback。这导致了无声文件或4KB(即空)文件。不要这样做。
  4. 另外,请务必使用传递给回调的ioData的副本
  5. recordingCallback之后AudioUnitRender进入bufferList

        AudioDeviceManager* THIS = (__bridge AudioDeviceManager *)inRefCon;
        if (THIS->isRecording) {
          ExtAudioFileWriteAsync(THIS->extAudioFileRef, inNumberFrames, bufferList);
        }
    
    1. 启动和停止录制功能,供参考:
    2. -(void)startRecording {

        NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *destinationFilePath = [documentsDirectory stringByAppendingPathComponent:kAudioFileName];
        CFURLRef destinationURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)destinationFilePath, kCFURLPOSIXPathStyle, false);
      
        OSStatus status;
      
        // create the capture file
        status = ExtAudioFileCreateWithURL(destinationURL, kAudioFileWAVEType, &audioFormat, NULL, kAudioFileFlags_EraseFile, &extAudioFileRef);
        if (status) NSLog(@"Error creating file with URL: %ld", status);
      
       // use the same "audioFormat" AudioStreamBasicDescription we used to set up RemoteIO in the first place
        status = ExtAudioFileSetProperty(extAudioFileRef, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &audioFormat);
      
        ExtAudioFileSeek(extAudioFileRef, 0);
        ExtAudioFileWrite(extAudioFileRef, 0, NULL);
      
        isRecording = YES;
      }
      

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *destinationFilePath = [documentsDirectory stringByAppendingPathComponent:kAudioFileName]; CFURLRef destinationURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)destinationFilePath, kCFURLPOSIXPathStyle, false); OSStatus status; // create the capture file status = ExtAudioFileCreateWithURL(destinationURL, kAudioFileWAVEType, &audioFormat, NULL, kAudioFileFlags_EraseFile, &extAudioFileRef); if (status) NSLog(@"Error creating file with URL: %ld", status); // use the same "audioFormat" AudioStreamBasicDescription we used to set up RemoteIO in the first place status = ExtAudioFileSetProperty(extAudioFileRef, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &audioFormat); ExtAudioFileSeek(extAudioFileRef, 0); ExtAudioFileWrite(extAudioFileRef, 0, NULL); isRecording = YES; }

      就是这样!