我正在使用 anAVAssetReaderOutput
从 中读取样本AVAsset
,对它们进行一些处理,然后使用 RemoteIO AU 播放结果。
问题是在调用AudioOutputUnitStop
暂停播放后,然后在进入后台并返回前台后,调用后音频不会再次开始AudioOutputUnitStart
。copyNextSampleBuffer
这是由于 的方法返回的错误,该方法AVAssetReaderOutput
被称为渲染管道的一部分。
调用后的status
属性为,其属性为Error Domain=AVFoundationErrorDomain Code=-11847 "Operation Interrupted" UserInfo=0x1d8b6100 {NSLocalizedRecoverySuggestion=停止其他操作并重试,NSLocalizedDescription=Operation Interrupted}AVAssetReader
copyNextSampleBuffer
AVAssetReaderStatusFailed
error
我正在寻找一种解决方案,它不会迫使我在回到前台后重新初始化整个管道 - 希望有这样一个解决方案,它AVAssetReader
可以在应用程序进入后台和返回后继续存在......
笔记
- 该应用程序有权在后台播放音频。
- 我正在处理音频中断 -
AVAudioSession
在AVAudioSessionDelegate
sendInterruptionWithFlags:
事件和应用程序激活时都将 my 设置为活动的。无论我是否这样做都没有区别,得到同样的错误。
一些代码:
音频播放器
@implementation AudioPlayer
...
// Audio Unit Setup
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
AudioComponent defaultOutput = AudioComponentFindNext(NULL, &desc);
AudioComponentInstanceNew(defaultOutput, &_audioUnit);
AudioStreamBasicDescription audioFormat;
FillOutASBDForLPCM(audioFormat, 44100, 2, 16, 16, false, false);
AudioUnitSetProperty(self.audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &audioFormat, sizeof(audioFormat));
AURenderCallbackStruct callbackStruct;
callbackStruct.inputProc = RenderCallback;
callbackStruct.inputProcRefCon = (__bridge void*)self;
AudioUnitSetProperty(self.audioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callbackStruct, sizeof(callbackStruct));
AudioUnitInitialize(self.audioUnit);
音频阅读器设置
@implementation AudioReader
...
NSError* error = nil;
self.reader = [AVAssetReader assetReaderWithAsset:self.asset error:&error];
NSDictionary *outputSettings = ...
self.readerOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[self.asset.tracks objectAtIndex:0] outputSettings:outputSettings];
[self.reader addOutput:self.readerOutput];
[self.reader startReading];
AudioReader 渲染方法,最终由 RenderCallback 函数调用
-(BOOL)readChunkIntoBuffer
{
CMSampleBufferRef sampleBuffer = [self.readerOutput copyNextSampleBuffer];
if ( sampleBuffer == NULL )
{
NSLog(@"Couldn't copy next sample buffer, reader status=%d error=%@, self.reader.status, self.reader.error);
return NO;
}
...
}