3

私はしばらくこれを克服しようとしました。音声を録音しようとしていますが、画面がロックされている間、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];

}

何かアイデアはありますか?

4

3 に答える 3

6

わかりましたので、見つけるのに長い時間がかかった私自身の質問への答えは次のとおりです。私が投稿したコードは良いですが、実際に機能するには、画面がロックされた後にバックグラウンドで機能する必要があります。このためには、アプリの plist ファイルに UIBackgroundModes 配列を追加し、そのオブジェクトの 1 つとして「オーディオ」を追加する必要があります。これにより、アプリがバックグラウンドで音声を処理できるようにシステムに指示します。

ドキュメントを見つけるのは それほど簡単ではありません。残念ながら、Apple はオーディオ セッション カテゴリのドキュメントで、特定のカテゴリがバックグラウンドで動作すると主張していることを明記していません。とにかく、この回答が同様の問題を抱えている他の人に利用できることを願っています...

于 2012-03-30T12:05:15.883 に答える
0

カテゴリを AVAudioSessionCategoryRecord としてAudioSessionに設定することを検討してください。

于 2012-03-29T10:07:12.413 に答える
0

録画が完了するまで画面ロックを無効にしてみませんか?

[UIApplication sharedApplication].idleTimerDisabled = YES;

// Do recording here

[UIApplication sharedApplication].idleTimerDisabled = NO;

完了したら、画面ロックを再度有効にすることを忘れないでください!

于 2012-03-29T03:44:03.480 に答える