1

我試圖克服這一段時間。我試圖錄制聲音,但AVAudioRecorder在屏幕鎖定時不錄製。一旦屏幕解鎖,它會繼續錄製,但屏幕鎖定時錄製的音頻永遠丟失。我無法找到我在做什麼錯: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]; 

} 

有什麼想法嗎?

+1

,以後會嘗試: 「你大概需要設置 UIBackgroundModes音頻的信息。 plist「 – user1258240 2012-03-29 22:02:10

+0

好吧 - 剛剛嘗試我以前的評論,它的工作!太糟糕了,我無法在AVAudioRecorder或AVAudioSession開發人員文檔中找到它。這裏有一個鏈接,我找到它的位置:[http://stackoverflow.com/questions/3848172/ios-multitasking-for-an-audio-recording-application] – user1258240 2012-03-29 22:30:56

+0

我希望我能標記自己是這個問題的回答者:) – user1258240 2012-03-29 22:32:06

回答

4

好了,回答我自己的問題,這我花了很長時間找出來,是以下幾點:我發佈的代碼很好,但實際上它需要在屏幕鎖定後在後臺工作。爲此,需要在應用程序的plist文件中添加UIBackgroundModes數組,並將「音頻」添加爲其對象之一。這告訴系統讓應用程序在後臺使用音頻。

這是not-so-easy to find documentation。不幸的是,蘋果沒有在他們的音頻會話類別的文檔中指定他們聲稱某些類別在後臺工作。無論如何,希望這個答案將提供給其他人誰也有類似的問題...發現這個在另一個線程

0

如何禁用屏幕鎖定,直到您完成錄製?

[UIApplication sharedApplication].idleTimerDisabled = YES; 

// Do recording here 

[UIApplication sharedApplication].idleTimerDisabled = NO; 

只要不要忘記當你完成後重新啓用屏幕鎖!

+1

不幸的是,我想要做的是完全相反:我想記錄很長一段時間,所以我想要做到這一點,而屏幕鎖定,以節省電池... – user1258240 2012-03-29 08:40:33

0

你可能要考慮AVAudioSessionCategoryRecord設置類別爲AudioSession

+0

我的壞,我沒有意識到你有已經嘗試過了。 – 2012-03-29 10:23:13

+0

是的,我做到了。雖然文件說它應該工作,但它不適合我.. – user1258240 2012-03-29 18:00:13

+0

有沒有人想出瞭如何解決這個問題。我確實打開了音頻的背景模式並設置了適當的類別。但它仍然不起作用。提前致謝 – 2016-02-26 11:07:35