• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

objective-c - 屏幕锁定时 AVAudioRecorder 不录制

[复制链接]
菜鸟教程小白 发表于 2022-12-12 20:04: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];

}

有什么想法吗?



Best Answer-推荐答案


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

这是 not-so-easy to find documentation .不幸的是,苹果没有在他们声称某些类别在后台工作的 Audio Session 类别的文档中具体说明。无论如何,希望这个答案对其他有类似问题的人有用......

关于objective-c - 屏幕锁定时 AVAudioRecorder 不录制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9918761/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap