我正在使用AVMutablecomposition用AVplayer播放一个在线视频文件。当我在浏览器上播放视频时,它播放得非常好,但我就是无法在我的iPad上播放它。
代码如下:
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 allocinitWithAsset:composition];”,那么我可以看到视频正在播放。
有谁知道如何在AVMutablecomposition中使用AVplayer播放视频?
发布于 2012-01-13 20:57:36
您的代码缺少几个错误检查,但它可以按原样工作。
除非你是在iPad中,否则这行代码会让玩家离开屏幕:
layerVideo.position = CGPointMake(1024/2, 768/2-10);如果是这样的话,试着选择正确的位置。
https://stackoverflow.com/questions/8834556
复制相似问题