【发布时间】:2023-03-29 04:21:01
【问题描述】:
我已经尝试克服这个问题一段时间了。我正在尝试录制声音,但屏幕锁定时 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];
}
有什么想法吗?
【问题讨论】:
-
在另一个线程中发现了这个,稍后会尝试:“你大概需要在 Info 中设置
UIBackgroundModes .plist"audio -
好的 - 刚刚尝试了我之前的评论,它成功了!太糟糕了,我在 AVAudioRecorder 或 AVAudioSession 开发人员文档中找不到它。这是我找到它的链接:[stackoverflow.com/questions/3848172/…
-
我希望我能将自己标记为这个问题的回答者 :)
-
您能否发布您尝试过的解决方案并将其标记为答案,以便对其他人有所帮助?
-
哦,我刚刚意识到我确实可以回答我自己的问题——我是新来的。好的,我会发帖的。
标签: objective-c ios audio-recording avaudiorecorder avaudiosession