ios - 如何向 AVPlayer 添加叠加层?
问题描述
我想在其中播放视频,AVPlayer
但想在其中打开AVPlayer
一个UIView
,以便我可以在其上添加一个叠加层。
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"Play" forState:UIControlStateNormal];
[button sizeToFit];
button.center = CGPointMake(320/2, 60);
[button addTarget:self action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)buttonPressed:(UIButton *)button
{
NSURL *urlVideoFile = [NSURL fileURLWithPath:@"https://www.rmp-streaming.com/media///bbb-360p.mp4"];
NSAssert(urlVideoFile, @"Expected not nil video url");
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.player = [AVPlayer playerWithURL:urlVideoFile];
_playerViewController.view.frame = self.view.bounds;
_playerViewController.showsPlaybackControls = YES;
[self.view addSubview:_playerViewController.view];
self.view.autoresizesSubviews = YES;
}
它正在播放器中播放视频,但我想在其中打开视频,UIView
以便可以在其上添加叠加层。
解决方案
以你的 UIView 的 IBOutlet 为例
IBOutlet UIView *videoView;
并调用此方法
-(void)buttonPressed:(UIButton *)button
{
NSURL *videoURL = [NSURL fileURLWithPath:filePath];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
playerLayer.frame = videoView.bounds;
[videoView.layer addSublayer:playerLayer];
[player play];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[player currentItem]];
}
- (void)playerItemDidReachEnd:(NSNotification *)notification
{
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
}
推荐阅读
- javascript - 在反应中删除待办事项列表中的项目
- perl - 按 REGEX 组将字符串拆分为 3
- html - 用于 HTML 名称输入验证的正则表达式不起作用
- java - LibGDX - 启用标记的字体和换行问题
- javascript - 暗光模式按钮
- scala - 在 Visual Studio Code 中,为什么“editor.parameterHints.enable”选项对弹出提示没有影响?
- node.js - 将 sass 添加到项目中 - 安装不起作用 - cb.apply 不是函数
- php - Xdebug 不会在 VSCode 上使用 docker 在断点处停止
- scala - 在 Zeppelin 上的 scala 中使用 Spark 拆分一列并将部分连接成一个新列
- arrays - Powershell CSV 导入。如果用户名匹配,则将值相加