Pregunta

I have an AudioPlayer implemented in an app. As soon as I call [AVAudioRecorder prepareToRecord]; the CPU usage goes up to 5%. Fine, because something useful is happening. But after [AVAudioRecorder stop]; is called, CPU drops to 3% and stays there.

This is the code where I implement the recorder:

- (void)prepareRecorder
{
    outputFileString = [NSString stringWithFormat:@"%@/%@", [self applicationDocumentsDirectory], @"audio.m4a"];

    NSArray *pathComponents = [NSArray arrayWithObjects:[self applicationDocumentsDirectory], @"audio.m4a", nil];
    outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];    
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];

    recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];
}

I don't know if it's possible to release/dealloc an object in ARC, but I tried

recorder = Nil;

How can I solve this? Or shouldn't I bother about the 3%?

¿Fue útil?

Solución

If You say it stays 3% no matter how much You start and stop AudioRecording, then there is no leak, but possibly SoundRecording framework has cached some private sound recording instances (something that is located and happens behind the Apple frameworks hood) and one of the reasons would be - next time to start recording faster than on first time. These cached instances would be deallocated in case sound player is not used anymore AND there would be a need of extra memory.

I think similarly is with AVPlayer and MFMessageComposeViewController (and probably many more frameworks/api's, which are heavy-to-run) - because - for both of these mentioned frameworks - on the first time I try to show SMS form, or play a video - there is a noticable delay. After that - there is no delay. So on first time it probably loads in cache something important, which can be auto-purged in case there is little memory available or application is closed.

So I think it is perfectly fine not to bother about this.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top