基本的AVAssetReader问题:读取视频样本时出现问题

6
我想通过AVAssetReader阅读视频样本,但是遇到了各种障碍。是否可以有人发布一些代码,从应用程序包中设置一个名为“Movie.m4v”的视频的AVAsset,并使用AVAssetReader循环遍历视频帧(CMSampleBufferRef)。
这是我现在正在使用的一些代码没有工作:
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");



}

资产阅读器初始化代码不应该太难正确编写。“崩溃”并没有说太多,那里到底发生了什么? - zoul
2个回答

11

由于[NSURL URLWithString:path],大多数情况下会导致崩溃。相反,您应该使用[NSURL fileURLWithPath:path]


0

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


我正在使用iOS 5,这可能是我看不到曲目的原因。 :( - devinross

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接