首页 文章

AVAudioRecorder - 正确的MPEG4 AAC录制设置

提问于
浏览
8

我有一个实时应用程序,估计有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 回答

  • 2

    它可能是许多与录制设置无关的事情 .

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

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

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

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

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

  • 0

    尝试这个我用来录制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];
    
  • 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)
        }
    })
    

    如果遇到类似问题,希望能节省一些时间 .

相关问题