首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >屏幕锁定时AVAudioRecorder不录制

屏幕锁定时AVAudioRecorder不录制
EN

Stack Overflow用户
提问于 2012-03-29 11:38:30
回答 3查看 2.5K关注 0票数 3

我试图克服这个问题已经有一段时间了。我正在尝试录制声音,但屏幕锁定时AVAudioRecorder无法录制。一旦屏幕被解锁,它将继续录制,但当屏幕被锁定时录制的音频将永远丢失。我在做的事情中找不到任何错误:

代码语言:javascript
复制
-(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];

}

有什么想法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-30 20:05:16

好了,我自己的问题的答案是这样的,我花了很长时间才找到答案:我发布的代码很好,但要真正工作,它需要在屏幕锁定后在后台工作。为此,需要在应用程序的plist文件中添加一个音频数组,并添加‘UIBackgroundModes’作为其对象之一。这会告诉系统让应用程序在后台处理音频。

这是not-so-easy to find documentation。不幸的是,苹果并没有在他们的音频会话分类文档中详细说明,他们声称某些分类在后台工作。无论如何,希望这个答案能为其他有类似问题的人提供……

票数 6
EN

Stack Overflow用户

发布于 2012-03-29 11:44:03

在你完成录制之前禁用屏幕锁定如何?

代码语言:javascript
复制
[UIApplication sharedApplication].idleTimerDisabled = YES;

// Do recording here

[UIApplication sharedApplication].idleTimerDisabled = NO;

完成后,不要忘记重新启用屏幕锁定!

票数 0
EN

Stack Overflow用户

发布于 2012-03-29 18:07:12

您可能需要考虑将类别设置为AudioSessionAVAudioSessionCategoryRecord

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9918761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档