简体   繁体   English

如何将叠加层添加到AVPlayer?

[英]How to add an overlay to a AVPlayer?

I want to play a video in AVPlayer but want to open the AVPlayer in a UIView so that I can add a overlay on it. 我想在AVPlayer播放视频,但想在UIView打开AVPlayer ,以便可以在其上添加覆盖。

- (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;
}

It's playing the video in a player, but I want to open the video in a UIView so that I can add the overlay on it. 它正在播放器中播放视频,但是我想在UIView打开视频,以便可以在其上添加叠加层。

Take IBOutlet of Your UIView for Example 以UIView的IBOutlet为例

IBOutlet UIView *videoView;

And Call this method 并调用此方法

-(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]; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM