Control buttons in AVPlayer and frame

5.7k Views Asked by At

I have to play a series of videos in my app. I am using AVQueuePlayer to play the videos using the code:

AVQueuePlayer *queuePlayer = [[AVQueuePlayer alloc] initWithItems:items];
AVPlayerLayer *myPlayerLayer  = [AVPlayerLayer playerLayerWithPlayer:queuePlayer];
myPlayerLayer.frame = CGRectMake(0, 0, 320, 350);
[self.view.layer addSublayer:myPlayerLayer];
[self.queuePlayer play];

I am facing two issues.

  1. When the video plays, the frame of player is not according to the given values(i.e. 0, 0, 320, 350).

  2. No control buttons(volume,play,stop, forward etc) are being showed as they are shown when we play video with MPMediaPlayer. Am i missing something for both cases? (I am using AVFoundation for the first time so these questions may seem stupid).

Thanks in advance.

2

There are 2 best solutions below

0
On BEST ANSWER

When the video plays, the frame of player is not according to the given values(i.e. 0, 0, 320, 350).

Apple's sample code doesn't use -playerLayerWithPlayer:. It creates a UIView subclass whose layer is an AVPlayerLayer. Perhaps there's a reason for that. I do that in my code and it works great. Take a look at the AVPlayerDemo code and try doing it that way.

No control buttons(volume,play,stop, forward etc) are being showed as they are shown when we play video with MPMediaPlayer. Am i missing something for both cases? (I am using AVFoundation for the first time so these questions may seem stupid).

AVPlayer does not provide these for you. That's what MPMoviePlayerController is for. If you want controls and you want to use AVPlayer, you'll need to create and manage the controls yourself.

0
On

As an alternative to Dave's answer to your first question...

If using auto layout, you can store, as a local property, a reference to the player's layer returned by playerLayerWithPlayer: and update its frame whenever the view controller's view lays out its subviews -

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    self.playerLayer.frame = self.view.layer.bounds;
}

Or if you're working in a UIView subclass -

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.playerLayer.frame = self.layer.bounds;
}

This assumes that you would like the player to fill the view; you can update the code if you have other intentions.