2012-01-12 46 views
1

我正在使用AVMutablecomposition与AVplayer一起播放在线视频文件。当我在浏览器上播放视频时,视频播放效果非常好,但我无法在iPad上播放。AVFoundation - 视频没有在AVMutablecomposition中与AVplayer一起玩

下面的代码:

self.composition = [AVMutableComposition composition]; 

NSURL *urlVideo = [NSURL URLWithString:@"{video location}"]; 
AVURLAsset *videoAsset = [[AVURLAsset alloc]initWithURL:urlVideo options:nil]; 

AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 

NSError *error = NULL; 

AVAssetTrack *videoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0]; 
CMTimeRange x = CMTimeRangeMake(kCMTimeZero, [videoAsset duration]); 
[compositionVideoTrack insertTimeRange:x ofTrack:videoTrack atTime:kCMTimeZero error:nil]; 

和代码来设置AVPlayer:

AVPlayerItem *playerItem = [[AVPlayerItem alloc]initWithAsset:composition]; 

self.player = [[AVPlayer alloc]initWithPlayerItem:playerItem]; 
AVPlayerLayer *layerVideo = [AVPlayerLayer playerLayerWithPlayer:self.player]; 
layerVideo.frame = self.view.layer.bounds; 
layerVideo.backgroundColor = [UIColor orangeColor].CGColor; 
layerVideo.position = CGPointMake(1024/2, 768/2-10); 

[self.view.layer addSublayer:layerVideo]; 
[self.player play]; 

如果我使用 “AVPlayerItem * playerItem = [AVPlayerItem playerItemWithAsset:videoAsset]。”而不是“AVPlayerItem * playerItem = [[AVPlayerItem alloc] initWithAsset:composition];”然后我可以看到播放的视频。

任何人都知道如何使用具有AVMutablecomposition的AVplayer播放视频?

回答

0

您的代码缺少一对错误检查,但它的工作原样。

除非你在一台iPad是该行会将玩家关闭屏幕:

layerVideo.position = CGPointMake(1024/2, 768/2-10); 

如果是这样的情况下,尝试用正确的位置。

+0

谢谢,但事实上,在我的部署中,我在layerVideo中添加了一个bg颜色,并且我可以看到该图层已添加到屏幕上(没有视频播放) – WilsonL 2012-01-16 03:29:25

+0

您可以看到背景颜色,并且仍然可以关闭视频屏幕。尝试使用带音频的视频或为图层添加边框(以检查整个图层是否在屏幕上)。你发布的代码在iOS 5中工作正常。 – djromero 2012-01-16 10:06:52