Мне нужно воспроизвести видео бесконечно (перезапуск видео по окончании) в моем приложении OpenGL. Для этого я пытаюсь использовать основу AV. Я создал AVAssetReader и AVAssetReaderTrackOutput, и я использую метод copyNextSampleBuffer для получения CMSampleBufferRef и создания текстуры OpenGL для каждого фрейма.AVFoundation для воспроизведения контура видео
NSString *path = [[NSBundle mainBundle] pathForResource:videoFileName ofType:type];
_url = [NSURL fileURLWithPath:path];
//Create the AVAsset
_asset = [AVURLAsset assetWithURL:_url];
//Get the asset AVAssetTrack
NSArray *arrayAssetTrack = [_asset tracksWithMediaType:AVMediaTypeVideo];
_assetTrackVideo = [arrayAssetTrack objectAtIndex:0];
//create the AVAssetReaderTrackOutput
NSDictionary *dictCompressionProperty = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id) kCVPixelBufferPixelFormatTypeKey];
_trackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:_assetTrackVideo outputSettings:dictCompressionProperty];
//Create the AVAssetReader
NSError *error;
_assetReader = [[AVAssetReader alloc] initWithAsset:_asset error:&error];
if(error){
NSLog(@"error in AssetReader %@", error);
}
[_assetReader addOutput:_trackOutput];
//_assetReader.timeRange = CMTimeRangeMake(kCMTimeZero, _asset.duration);
//Asset reading start reading
[_assetReader startReading];
И в -update методе моего GLKViewController я называю следующее:
if (_assetReader.status == AVAssetReaderStatusReading){
if (_trackOutput) {
CMSampleBufferRef sampleBuffer = [_trackOutput copyNextSampleBuffer];
[self createNewTextureVideoFromOutputSampleBuffer:sampleBuffer]; //create the new texture
}
}else if (_assetReader.status == AVAssetReaderStatusCompleted) {
NSLog(@"restart");
[_assetReader startReading];
}
Все отлично работает до AVAssetReader находится в состоянии чтения, но когда он закончил читать, и я попытался перезапустить AVAssetReading с новым вызовом [_assetReader startReading], сбой приложения без вывода. Что я делаю неправильно? Правильно перезапустить AVAssetReading, когда он завершит чтение?
Обновление, вот реальное рабочее решение, использующее новые API доступа к оборудованию VideoToolbox в iOS: http://stackoverflow.com/a/33335884/763355 – MoDJ