从应用内播放video?

我正尝试使用AVPlayer从我的应用程序播放本地video。 我到处都找,但找不到任何有用的教程/演示/文档。 这是我正在尝试,但它不是玩。 任何想法为什么? 该URL是有效的,因为我使用相同的一个成功地使用MPMoviePlayer播放video。

Video *currentVideo = [videoArray objectAtIndex:0]; NSString *filepath = currentVideo.videoURL; NSURL *fileURL = [NSURL fileURLWithPath:filepath]; AVURLAsset *asset = [AVURLAsset assetWithURL: fileURL]; AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset]; self.player = [[AVPlayer alloc] initWithPlayerItem: item]; AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player]; self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone; layer.frame = CGRectMake(0, 0, 1024, 768); [self.view.layer addSublayer: layer]; [self.player play]; 

我相信问题是,你假设在执行这行AVURLAsset *asset = [AVURLAsset assetWithURL: fileURL]; 该资产已准备好使用。 AV Foundation编程指南中指出的情况并非如此:

  You create an asset from a URL using AVURLAsset. Creating the asset, however, does not necessarily mean that it's ready for use. To be used, an asset must have loaded its tracks. 

创build资产后,尝试加载它的轨道:

 AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil]; NSString *tracksKey = @"tracks"; [asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler: ^{ NSError *error; AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error]; if (status == AVKeyValueStatusLoaded) { // At this point you know the asset is ready self.playerItem = [AVPlayerItem playerItemWithAsset:asset]; ... } }]; 

请参阅此链接查看完整的示例。

希望这可以帮助!