Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

ios - AVAudioRecorder not working on iPhone 5S

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...