Mat*_*att 4 avfoundation avaudioplayer avaudiorecorder
以下是我的记录按钮方法的内容.它创建了所需的文件,但无论我记录多长时间,创建的文件总是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];
}
Run Code Online (Sandbox Code Playgroud)
小智 6
在开始录制之前,请将应用的AudioSession设置为支持录制的会话类型之一.例如,在开始录制之前调用它:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error];
Run Code Online (Sandbox Code Playgroud)
如果您尝试将AudioSession设置为不支持录制的类型进行录制,则代码似乎执行正常,但会导致您描述的空4kb音频文件.
要稍后播放该文件,请确保将类别设置回支持播放的类别,例如AVAudioSessionCategoryPlayback或AVAudioSessionCategoryPlayAndRecord.
| 归档时间: |
|
| 查看次数: |
936 次 |
| 最近记录: |