AVAudioRecorder - 正确的MPEG4 AAC录制设置

时间:2012-07-05 15:35:57

标签: ios ios5 avaudioplayer avaudiorecorder avaudiosession

我有一个实时应用程序,估计有15%的用户报告记录功能不起作用。这在我们的测试设备上没有发生,但报告显示问题是prepareToRecord返回NO。我很难找到AAC格式的样本设置。我的任何设置都关闭了吗?应用程序需要iOS5并使用ARC。

 AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
 [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
 [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
 [NSNumber numberWithInt:1], AVNumberOfChannelsKey,
 [NSNumber numberWithInt:AVAudioQualityHigh], AVSampleRateConverterAudioQualityKey,
 [NSNumber numberWithInt:128000], AVEncoderBitRateKey,
 [NSNumber numberWithInt:16], AVEncoderBitDepthHintKey,
 nil];

NSString *fileName = [NSString stringWithFormat:@"%@%@.caf", verseGUID, bRecordingReference ? @"_ref" : @""];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[Utilities sharedInstance] documentsDirectoryPath], fileName]];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
if([audioRecorder prepareToRecord]){
    [audioRecorder record];
}else{
    int errorCode = CFSwapInt32HostToBig([error code]);
    NSLog(@"Error: %@ [%4.4s])", [error localizedDescription], (char*)&errorCode);
}  

3 个答案:

答案 0 :(得分:3)

可能有很多事情与录音设置无关。

你想要回答的真正问题似乎是:什么会导致录音不发生?

audioRecorder可能是nil或者audioRecorder prepareToPlay可能返回NO。前者似乎更有可能。

传递给initWithURL的url可能格式错误: - 您是否通过使用verseGUID,bRecordReference值进行测试?也许你的设备永远不会有坏的verseGUID,但没有录音的设备有一个零/空的verseGUID。这可能会导致文件名只是“.caf”。

您似乎拥有自己的类方法[Utilities sharedInstance]。这可能是因为某些原因在你的设备上工作而不是在发生故障的设备上?如果是这样,你可能会要求在你不想要的时候在顶级目录中录制。

你可以让你的测试人员进入“测试版”列表吗?注册一些像TestFlight或Hockey Kit这样的东西,让一个或多个记录失败的用户也注册,然后上传你的应用程序的测试版,其诊断信息会在屏幕上显示“错误”。这可能是最明显的。我使用testflightapp.com只是因为它是我尝试过的第一个,我很容易管理并且对我的beta测试者来说非常轻松。

答案 1 :(得分:2)

尝试使用我用来录制MPEG 4 AAC格式的设置......它的工作正常......

NSString *tempDir = NSTemporaryDirectory();
NSString *soundFilePath = [tempDir stringByAppendingPathComponent:@"sound.m4a"];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
                                  [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
                                  [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                                  [NSNumber numberWithFloat:8000.0], AVSampleRateKey,
                                  nil];

答案 2 :(得分:0)

道歉这是一个相反的问题,但我和一些用户无法录制的问题非常相似,我认为这与音频设置有关。

然而对我来说,事实证明他们拒绝了访问麦克风的许可。这意味着prepareToRecord正在运行(并截断文件),record报告它正在运行,但实际上它并没有记录任何内容。

当我想录制时我用过这个:

AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
    if granted {
        // can record - create AVAudioRecorder here

    } else {
        // can't record - update UI (or react accordingly)
    }
})

希望如果他们遇到类似问题,可以节省一些时间。