AVAudioRecorder not working on iPhone 5S

喜夏-厌秋 提交于 2019-11-27 14:06:32

问题


So I'm using an AVAudioRecorder to record audio alongside an AVCaptureSession that is recording video (I know this is odd, but for my situation I need to record them seperately).

Everything works fine on every device, except for my iPhone 5S. It records without error, but the file that is saved to disk is corrupted or something. When I access the file system on my mac and try and play the m4a file with VLC or Quicktime, I get a "format of the file cannot be detected" error. Here is how I am initializing my AVAudioRecorder and recording my audio:

// Prepare the audio session
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:nil];

 // Setup audio recording
 NSDictionary *recordSettings = @{AVFormatIDKey: @(kAudioFormatMPEG4AAC),
                                      AVEncoderAudioQualityKey: @(AVAudioQualityLow),
                                      AVEncoderBitRateKey: @16,
                                      AVNumberOfChannelsKey: @1,
                                      AVSampleRateKey: @22050.0f};

 NSError *audioRecorderError;

 NSURL *audioFileURL = [[self.outputFileURL URLByDeletingPathExtension] URLByAppendingPathExtension:@"m4a"];

 self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:audioFileURL
                                                              settings:recordSettings
                                            error:&audioRecorderError];

self.audioRecorder.delegate = self;

 if (audioRecorderError) {
      CCLog(@"Error while initializing the audio recorder... Skipping sound recording!");
 }
else {
    if (![self.audioRecorder prepareToRecord]) {
        CCLog(@"Error preparing to record");
    }
    if (![self.audioRecorder record]) {
        CCLog(@"Error recording");
    }
}

Again, this works on all devices aside from the 5S. Anyone know what could be causing this?


回答1:


Did some more digging and I apparently found the solution. I simply got rid of AVEncoderBitRateKey and everything works fine. So now my recordSettings dictionary looks like this:

// Setup audio recording
NSDictionary *recordSettings = @{AVFormatIDKey: @(kAudioFormatMPEG4AAC),
                                  AVEncoderAudioQualityKey: @(AVAudioQualityLow),
                                  AVNumberOfChannelsKey: @1,
                                  AVSampleRateKey: @22050.0f};

Still not sure why this would be the case only on an iPhone 5S. Again, I've tested on all other devices running iOS6 and iOS7, and the old settings dictionary works fine on everything except for the 5S. Now that I've removed the AVEncoderBitRateKey and value, it also works on the 5S.




回答2:


It looks like it's actually an interaction between AVNumberOfChannelsKey and AVEncoderBitRateKey. I can't seem to record above 64000Kbps when the # of channels = 1, but when it = 2, I can use 128000...




回答3:


I had this same problem - and the answer proved to be related to AVAudioSession. I needed to set the audio session category to PlayAndRecord. Other parts of my application were setting it to Ambient. This post https://stackoverflow.com/a/10287793/1009125 gave me the answer. Hope this helps someone, I lost a Sunday to this one.



来源:https://stackoverflow.com/questions/19192316/avaudiorecorder-not-working-on-iphone-5s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!