-->

AVAudioRecorder - 正确MPEG4 AAC录音设置(AVAudioRecorder

2019-07-30 06:55发布

我有与用户报告的记录功能不能正常工作,估计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);
}  

Answer 1:

它可以不必与记录的设置做很多事情。

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

audioRecorder可能是零或audioRecorder prepareToPlay可以返​​回NO。 前者似乎可能性更大些。

传递给initWithURL的URL可以是畸形: - 你通过与verseGUID,bRecordReference值打测试? 也许你的设备从不有一个坏verseGUID,但其上没有记录恰好有一个零/空verseGUID设备。 这可能会导致文件名是简单地“的.caf”。

你似乎有自己的类方法[实用程序sharedInstance。 可能这个工作对你的设备,但不是失败的设备由于某种原因? 如果是的话,你可能会被要求在顶层目录记录,当你不是故意的。

你可以得到你有测试人员到“测试版”名单? 报名参加类似TestFlight或曲棍球套件,让用户的一个或多个有故障记录还注册,然后上传应用程序的测试与投入的对话在屏幕上与得到的“错误”诊断。 这可能是最明显的。 我使用testflightapp.com只因为它是第一个我想,这是很容易的,我管理和相当痛苦我的beta测试。



Answer 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];


Answer 3:

道歉,这是稍微切,但我刚刚有一个非常类似的问题与许多用户无法记录,我认为是与音频设置。

但是对我来说,原来,他们否认权限访问麦克风。 这意味着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)
    }
})

希望这样可以节省别人一些时间,如果他们碰上了类似的问题。



文章来源: AVAudioRecorder - Proper MPEG4 AAC Recording Settings