AVFoundation para reproduzir um loop de vídeo
Eu preciso reproduzir um vídeo indefinidamente (reiniciar o vídeo quando ele termina) no meu aplicativo OpenGL. Para fazer isso, estou tentando utilizar a base AV. Eu criei um AVAssetReader e um AVAssetReaderTrackOutput e eu utilizo o método copyNextSampleBuffer para obter CMSampleBufferRef e criar uma textura OpenGL para cada quadro.
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];
E no método de atualização do meu GLKViewController eu chamo o seguinte:
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];
}
Tudo funciona bem até que o AVAssetReader esteja no status de leitura, mas quando ele terminou de ler e tentei reiniciar o AVAssetReading com uma nova chamada [_assetReader startReading], o aplicativo falha sem saída. O que estou fazendo errado? É correto reiniciar um AVAssetReading quando completar sua leitura?