0

我经常使用 AVRecorder 和 AVPlayer。我看到他们俩都延迟了,当我回到我的应用程序时,在她处于后台之后 - 例如,如果我试图录制,它需要 4 秒才能开始。

在我的代码中,我准备在录制之前录制,但是当回到应用程序时,它没有帮助。

我能做些什么来改进它?

这是在应用程序打开时完成的(在开始时,而不是从后台返回时!)

-(void)prepareToRecord
{
    NSArray *dirPaths;
    NSString *docsDir;

    NSString *sound=[NSString stringWithFormat:@"sound%d.caf",[memoryInstnace getNextFreePlace]];
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ;
    docsDir = [dirPaths objectAtIndex:0];
    currentSoundFilePath = [docsDir stringByAppendingPathComponent:sound];
    NSURL *soundFileURL = [NSURL fileURLWithPath:currentSoundFilePath];



    NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                              [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                              [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                              [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                              nil];

    NSError *error;

    recorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:settings error:&error];

    //to not having a delay befor record
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

    if (recorder)
    {
        [recorder prepareToRecord];
        recorder.meteringEnabled = YES;

    }
    else
        NSLog( @"error in recordings"  );


}

谢谢 !

4

1 回答 1

0

如果另一个应用程序通过启动一个活动的 AVAudioSession 来控制麦克风,那么 iOS 会淡出该应用程序的控制权,并在您激活会话时将其提供给您。这可能需要一两秒钟。这是正在发生的事情吗?或者是其他什么让你慢下来?

于 2012-10-02T21:46:23.737 回答