以下是我的录制按钮方法的内容。它会创建所需的文件,但无论我录制多长时间,创建的文件始终是 4kb 和 0 秒的长度,我无法弄清楚它为什么无效。对于averagePowerPerChannel,计量也返回-120,我假设它是因为文件已损坏。
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error;
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
stringByAppendingPathComponent:@"sound.caf"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
self.shotRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&error];
self.shotRecorder.delegate = self;
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[self.shotRecorder prepareToRecord];
self.shotRecorder.meteringEnabled = YES;
[self.shotRecorder record];
shotTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
}