2011-06-21 23 views
6

我正在通过AVAssetReader阅读视频样本,我遇到了各种路障。可以从应用程序包中的一个名为“Movie.m4v”的视频设置AVAsset,并使用AVAssetReader循环播放视频帧(CMSampleBufferRef)。阅读视频样本的基本AVAssetReader问题

下面是一些我现在使用代码不起作用的:

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Movie.m4v"]; 
AVAsset *avAsset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:path] options:nil]; 
NSError *error = nil; 
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:avAsset error:&error]; // crashing right around here! 

// I've gotten avassetreader to not crash upon initialization by grabbing an asset from the ALAssetsLibrary but it says it has 0 tracks! 

NSArray *videoTracks = [avAsset tracksWithMediaType:AVMediaTypeVideo]; 
AVAssetTrack *videoTrack = [videoTracks objectAtIndex:0]; 
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]; 
AVAssetReaderTrackOutput *asset_reader_output = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack outputSettings:options]; 
[reader addOutput:asset_reader_output]; 
[reader startReading]; 

CMSampleBufferRef buffer; 
while ([reader status]==AVAssetReaderStatusReading){ 

    buffer = [asset_reader_output copyNextSampleBuffer]; 
    NSLog(@"READING"); 



} 
+0

资产阅读器初始化代码不应该很难正确。 “崩溃”并不多说,那里到底发生了什么? – zoul

回答

10

因为[NSURL URLWithString:path]的最有可能崩溃。您应该使用[NSURL fileURLWithPath:path]

+0

它工作正常 – Thangavel

0

您需要让您的AVAsset在使用轨道之前异步加载其轨道属性。

+0

我使用的是iOS 5,所以可能是我没有看到任何曲目的原因。 :( – devinross