我需要在 OpenGL 应用程序中无限期地重现视频(视频结束时重新启动)。
为此,我尝试利用 AV 基础。
我创建了一个 AVAssetReader 和一个 AVAssetReaderTrackOutput,并利用 copyNextSampleBuffer 方法来获取 CMSampleBufferRef 并为每个帧创建一个 OpenGL 纹理。
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];
在我的 GLKViewController 的 -update 方法中,我调用以下内容:
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 处于读取状态,但是当它完成读取并且我尝试使用新调用 [_assetReader startReading] 重新启动 AVAssetReading 时,应用程序崩溃而没有输出。
我做错了什么?当 AVAssetReading 完成读取时重新启动它是正确的吗?
AVAssetReader
不支持查找或重新启动,它本质上是一个顺序解码器。你必须创建一个新的AVAssetReader
反对再次读取相同的样本。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)