我正在使用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 alloc] initWithAsset:composition];”然后我就可以看到视频播放了。
任何人都知道如何使用带有AVMutablecomposition的AVplayer播放视频吗?
答案 0 :(得分:0)
您的代码缺少一些错误检查,但它按原样运行。
除非你在iPad上,否则这一行会将播放器发送到屏幕外:
layerVideo.position = CGPointMake(1024/2, 768/2-10);
如果是这种情况,请尝试使用正确的位置。