如何为AVPlayer添加叠加层?

时间:2018-05-21 11:02:56

标签: ios objective-c avplayer

我想在let dp = new DatePipe('en-EN' ); dp.transform(this.MyDate, 'yyyy-MM-dd')) 中播放视频,但想要在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; } 中打开视频,以便我可以在其上添加叠加层。

1 个答案:

答案 0 :(得分:0)

以你的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]; 
}