Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何为AVPlayer添加覆盖?_Ios_Objective C_Avplayer - Fatal编程技术网

Ios 如何为AVPlayer添加覆盖?

Ios 如何为AVPlayer添加覆盖?,ios,objective-c,avplayer,Ios,Objective C,Avplayer,我想在AVPlayer中播放视频,但想在ui视图中打开AVPlayer,以便在其上添加覆盖 - (void)viewDidLoad { [super viewDidLoad]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:@"Play" forState:UIControlStateNormal]; [button sizeToFit];

我想在
AVPlayer
中播放视频,但想在
ui视图中打开
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;
}

它在播放机中播放视频,但我想在
UIView
中打开视频,以便在其上添加覆盖层。

以UIView的IBOutlet为例

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

您只需要添加“addSublayer”而不是“addSubview”。@shiwani如果答案解决了您的问题,您应该通过单击答案左侧的复选标记来指示您的问题已解决。