我试图克服这个问题已经有一段时间了。我正在尝试录制声音,但屏幕锁定时AVAudioRecorder无法录制。一旦屏幕被解锁,它将继续录制,但当屏幕被锁定时录制的音频将永远丢失。我在做的事情中找不到任何错误:
-(void) startRecording
{
// Begin the recording session.
_session = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
NSError *startRecordError;
[_session setActive:YES error:&startRecordError];
[self GKLog:[NSString stringWithFormat:@"recorder session error? :%@", startRecordError]];
[_session setCategory: AVAudioSessionCategoryRecord error: &setCategoryError];
if (setCategoryError) { NSLog(@"some error");}
//set me as delegate
_session.delegate=(id <AVAudioSessionDelegate>) self;
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue :[NSNumber numberWithInt:8] forKey:AVEncoderBitRateKey];
[recordSetting setValue:[NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
if (!self.currentPath)
{
NSLog(@"can't record, no path set!");
return;
}
NSError *error;
NSURL *url=[NSURL fileURLWithPath:self.currentPath];
//Setup the recorder to use this file and record to it.
_recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&error];
[self GKLog:[NSString stringWithFormat:@" recorder:%@",_recorder]];
_recorder.delegate=(id <AVAudioRecorderDelegate>) self;
[_recorder prepareToRecord];
//Start the actual Recording
[_recorder record];
}有什么想法吗?
发布于 2012-03-30 20:05:16
好了,我自己的问题的答案是这样的,我花了很长时间才找到答案:我发布的代码很好,但要真正工作,它需要在屏幕锁定后在后台工作。为此,需要在应用程序的plist文件中添加一个音频数组,并添加‘UIBackgroundModes’作为其对象之一。这会告诉系统让应用程序在后台处理音频。
这是not-so-easy to find documentation。不幸的是,苹果并没有在他们的音频会话分类文档中详细说明,他们声称某些分类在后台工作。无论如何,希望这个答案能为其他有类似问题的人提供……
发布于 2012-03-29 11:44:03
在你完成录制之前禁用屏幕锁定如何?
[UIApplication sharedApplication].idleTimerDisabled = YES;
// Do recording here
[UIApplication sharedApplication].idleTimerDisabled = NO;完成后,不要忘记重新启用屏幕锁定!
发布于 2012-03-29 18:07:12
您可能需要考虑将类别设置为AudioSession的AVAudioSessionCategoryRecord
https://stackoverflow.com/questions/9918761
复制相似问题