Search code examples
iosobjective-ciphoneuibuttonsplash-screen

How to add the UIButton present in storyboard to the top of AVPlayer in iOS?


I am doing the implementation of video splash similar to Uber in my application. I had added two buttons in the storyboard at the bottom part with equal spacing in storyboard. Now i am adding AVPlayer to play a video present in NSBundle. While running the application the video is playing but i cant see my buttons at the bottom part. Why it is happening? Please suggest..

   - (void)createVideoPlayer {

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"welcome_video" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:filePath];

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];

    self.player = [AVPlayer playerWithPlayerItem:playerItem];
    self.player.volume = PLAYER_VOLUME;

    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    playerLayer.videoGravity = UIViewContentModeScaleToFill;
    playerLayer.frame = self.playerView.layer.bounds;
    [self.playerView.layer addSublayer:playerLayer];

    [self.player play];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
}

- (void)moviePlayDidEnd:(NSNotification*)notification{

    AVPlayerItem *item = [notification object];
    [item seekToTime:kCMTimeZero];
    [self.player play];
}

The screenshot of buttons in storyboard: enter image description here

The output in simulator:

enter image description here

You can see buttons are missing and Note: constraints of these buttons are correct and i can see them in every preview...


Solution

  • You need to call bringSubviewToFront to bring your Button outlet front and visible on AVPlayer after adding AVPlyer instance in your view.

    [self.view bringSubviewToFront:buttonOutlet];