问题描述
我需要在OpenGL应用程序中无限期地复制视频(在结束时重新启动视频).为此,我正在尝试利用AV基础.我创建了一个AVAssetReader和AVAssetReaderTrackOutput,并使用copyNextSampleBuffer方法获取CMSampleBufferRef并为每个帧创建一个OpenGL纹理.
I need to reproduce a video indefinitely (restarting the video when it ends) in my OpenGL application.To do so I'm trying to utilize AV foundation.I created an AVAssetReader and an AVAssetReaderTrackOutput and I utilize the copyNextSampleBuffer method to get CMSampleBufferRef and create an OpenGL texture for each frame.
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方法中,我调用以下内容:
And in -update method of my GLKViewController I call the following:
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是正确的吗?
All work fine until the AVAssetReader is in the reading status but when it finished reading and I tried to restart the AVAssetReading with a new call [_assetReader startReading], the application crash without output.What I'm doing wrong? It is correct to restart an AVAssetReading when it complete his reading?
推荐答案
AVAssetReader
不支持查找或重新启动,它本质上是一个顺序解码器.您必须创建一个新的AVAssetReader
对象才能再次读取相同的样本.
AVAssetReader
doesn't support seeking or restarting, it is essentially a sequential decoder. You have to create a new AVAssetReader
object to read the same samples again.
这篇关于AVFoundation重现视频循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!