Melanie Journe
Melanie Journe

Reputation: 1379

AVPlayerController - Buttons not working on full screen

I'm trying to understand how AVPlayer & AVPlayerController works. I'm embedded my player in a view so my player has two mode : little & full screen. When it's little the play/plause button and full screen button work very well. But once it's full screen, the following buttons has no effect : play, pause, done, minimise.

Here is my code :

    let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
    let avPlayer:AVPLayer = AVPlayer(url: videoURL! as URL)
    let playerController = AVPlayerViewController()
    playerController.player = avPlayer
    playerController.view.backgroundColor = UIColor.racingGreenColor();
    playerController.view.translatesAutoresizingMaskIntoConstraints = false;
    playerController.showsPlaybackControls = true;
    self.middleView.addSubview(playerController.view)
    //avPlayer.play()

    self.middleView.addConstraint(NSLayoutConstraint(item: playerController.view, attribute: .width, relatedBy: .equal, toItem: self.middleView, attribute: .width, multiplier: 1, constant: 0));
    self.middleView.addConstraint(NSLayoutConstraint(item: playerController.view, attribute: .height, relatedBy: .equal, toItem: self.middleView, attribute: .height, multiplier: 1, constant: 0));
    self.middleView.addConstraint(NSLayoutConstraint(item: playerController.view, attribute: .centerX, relatedBy: .equal, toItem: self.middleView, attribute: .centerX, multiplier: 1, constant: 0));
    self.middleView.addConstraint(NSLayoutConstraint(item: playerController.view, attribute: .centerY, relatedBy: .equal, toItem: self.middleView, attribute: .centerY, multiplier: 1, constant: 0));

Can someone help me please ? Is there a delegate for the 4 methods i'm interested in ?

Upvotes: 1

Views: 2708

Answers (3)

Melanie Journe
Melanie Journe

Reputation: 1379

self.addChildController(playerController);

I've add it on your main view controller and it does the trick for "Done" and "Minimise".

Upvotes: 1

Dhara Ahuja
Dhara Ahuja

Reputation: 1

You can add AVPlayer as a layer, and add your own custom buttons

player = [[[AVPlayer alloc] init...

playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
CGSize size = self.view.bounds.size;
float x = size.width/2.0-187.0;
float y = size.height/2.0 - 125.0;

playerLayer.frame = CGRectMake(x, y, 474, 320);
[playerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.view.layer addSublayer:playerLayer];
[player play];

Upvotes: 0

Dhara Ahuja
Dhara Ahuja

Reputation: 1

It works for me as self.present(playerController, animated: true, completion:nil)

Since it is a controller, we should use it as a controller rather than as sub viewing it.

Upvotes: 0

Related Questions