AVPlayerLayer没有显示AVPlayer视频吗? [英] AVPlayerLayer isn't showing AVPlayer video?
问题描述
让AVPlayer视频内容显示在人们视野中的窍门是什么?
What's the trick to getting AVPlayer video content to show up in one's view?
我们正在使用以下AVPlayer代码,但屏幕上没有任何显示.我们知道视频在那里,因为我们能够使用MPMoviePlayerController来显示它.
We are using the following AVPlayer code but nothing is appearing on screen. We know the video is there because we were able to show it using an MPMoviePlayerController.
这是我们正在使用的代码:
Here is the code we are using:
AVAsset *asset = [AVAsset assetWithURL:videoTempURL];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:item];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
// layer.frame = self.view.frame;
[self.view.layer addSublayer:layer];
layer.backgroundColor = [UIColor clearColor].CGColor;
//layer.backgroundColor = [UIColor greenColor].CGColor;
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[player play];
我们当前视图的图层设置不正确吗?
Are we setting the layer improperly for the current view?
推荐答案
事实证明,AVPlayer需要使用自己的上下文视图才能播放.
It turns out that AVPlayer needs its own contextual view in order to play.
我们添加了此代码,现在可以播放视频了.不幸的是,与MPMoviePlayerController不同,AVPlayer没有内置控件.目前尚不清楚苹果为什么不赞成使用具有非标准化视频播放选项的工具.
We added this code and now the video plays. Unfortunately, AVPlayer has no built in controls unlike MPMoviePlayerController. It is unclear why Apple is deprecating towards a tool that will have non-standardized video playback options.
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0, 320.0f, 200.0f)];
layer.frame = self.view.frame;
[containerView.layer addSublayer:layer];
[self.view addSubview:containerView];
layer.backgroundColor = [UIColor greenColor].CGColor;
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[player play];
这篇关于AVPlayerLayer没有显示AVPlayer视频吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!