本文介绍了在后台录制语音使用AVAudioRecorder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用记录在后台用户语音 AVAudioRecorder 但每次我得到的时间 NO 同时呼吁 recordForDuration 方法。我想补充声音来 UIBackgroundModes 但这没有帮助。
顺便说一句,一切工作在前台完美。我初始化 AVAudioRecorder 使用此code:

I'm trying to record user voice in background using AVAudioRecorder but every time i get NO while calling recordForDuration method. I add "audio" to UIBackgroundModes but this don't help.By the way, Everything works perfect in foreground. I'm initializing AVAudioRecorder using this code:

NSError* error;
if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error])
{
    NSLog(@"Error while setting audio session category. Code - %d, description - \"%@\".", [error code], [error localizedDescription]);
    return;
}

NSURL* outputFileURL = [VKMFileSystemHelper uniqueFileInPath:[[VKMFileSystemHelper subdirectoryInsideLibrary:DIR_AUDIO] path] withExtension:@"m4a"];

NSDictionary* recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
                                    [NSNumber numberWithInt:AVAudioQualityMin]   , AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:16]                  , AVEncoderBitRateKey,
                                    [NSNumber numberWithInt:1]                   , AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:12000.0]           , AVSampleRateKey,
                                    nil];

_recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSettings error:&error];
[_recorder setDelegate:self];

if (error)
{
    NSLog(@"Error occured during audio recorder initialization. Error code - %d, description - \"%@\".", [error code], [error localizedDescription]);
}
else
{
    NSLog(@"Start recording.");
    if ([_recorder recordForDuration:[shedule execLength]])
    {
        NSLog(@"Record started.");
    }
    else
    {
        NSLog(@"Record failed.");
    }
}

我如何能记录从麦克风的背景是什么?这可能吗?

How can i record from microphone from background ? Is this possible?

推荐答案

试试这个,

        ***Appdelegate.m***

- (void)applicationDidEnterBackground:(UIApplication *)application
{
__block UIBackgroundTaskIdentifier task = 0;
task=[application beginBackgroundTaskWithExpirationHandler:^{
    NSLog(@"Expiration handler called %f",[application backgroundTimeRemaining]);
    [application endBackgroundTask:task];
    task=UIBackgroundTaskInvalid;
}];

}

这篇关于在后台录制语音使用AVAudioRecorder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 02:41