问题
My app reads audio and plays it back in a producer / consumer setup. The consumer thread requests new samples to render to hardware. The producer thread reads audio data from disk into its buffer using AVAssetReader. The producer thread runs in a loop, checking if more samples need to be read. The producer's buffer size is equal to 4 seconds of audio.
When I instruct my app to buffer audio, samples are read successfully without error. When I trigger my producer thread to begin rendering audio, the 4 second buffer is played back perfectly, then silence. Further investigation reveals that my asset reader fails upon the start of playback, thus no further samples where read after the initial buffering:
CMSampleBufferRef ref = [readaudiofile copyNextSampleBuffer];
if (ref == NULL && filereader.status == AVAssetReaderStatusFailed) {
NSLog(@"reader failed: %@", filereader.error);
}
// this produces:
// {NSLocalizedFailureReason=An unknown error occurred (-12785),
// NSUnderlyingError=0x161f60 "The operation couldn’t be completed.
// (OSStatus error -12785.)", NSLocalizedDescription=The operation
// could not be completed}
My producer thread code is identical to the functional code example MusicLibraryRemoteIODemo. My consumer thread code is different, but I've compared the two line by line for a couple days and can't seem to find the hitch. Any idea what might be causing the AVAssetReader reader to fail?
This post describes similar solutions and it concluded that the Audio Session needed to be set up properly (although it doesn't say how). Is there any connection between AVAudioSesion configuration and the behavior of AVAssetReader?
回答1:
I was getting this same error, I posted this answer on the other thread as well
- (void)setupAudio {
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive: YES error:&activationError];
NSLog(@"setupAudio ACTIVATION ERROR IS %@", activationError);
[[AVAudioSession sharedInstance] setPreferredIOBufferDuration:0.1 error:&activationError];
NSLog(@"setupAudio BUFFER DURATION ERROR IS %@", activationError);
}
来源:https://stackoverflow.com/questions/5775548/avassetreader-reads-audio-then-fails