我已经尝试克服这个问题一段时间了。我正在尝试录制声音,但 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];
}
有什么想法吗?
好的,我花了很长时间才找到我自己的问题的答案如下:我发布的代码很好,但要实际工作,它需要在屏幕锁定后在后台工作。为此,需要在应用程序的 plist 文件中添加一个 UIBackgroundModes 数组,并将“音频”添加为其对象之一。这告诉系统让应用程序在后台处理音频。
这是 not-so-easy to find documentation .不幸的是,苹果没有在他们声称某些类别在后台工作的 Audio Session 类别的文档中具体说明。无论如何,希望这个答案对其他有类似问题的人有用......
关于objective-c - 屏幕锁定时 AVAudioRecorder 不录制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9918761/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |