AVPlayerController - 按钮不能在全屏幕上工作

时间:2016-11-25 08:33:08

标签: ios swift avplayer avplayerviewcontroller avplayerlayer

我试图了解AVPlayer& AVPlayerController工作。 我在一个视图中嵌入了我的播放器,因此我的播放器有两种模式:little&全屏。 当它很小时,播放/播放按钮和全屏按钮工作得很好。但是一旦全屏显示,以下按钮无效:播放,暂停,完成,最小化。

这是我的代码:

    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));
是的,有人能帮帮我吗? 是否有我感兴趣的4种方法的代表?

3 个答案:

答案 0 :(得分:1)

self.addChildController(playerController);

我已将它添加到您的主视图控制器上,它可以实现“完成”和“最小化”。

答案 1 :(得分:0)

它适用于我作为self.present(playerController,animated:true,completion:nil)

由于它是一个控制器,我们应该将它用作控制器而不是子视图。

答案 2 :(得分:0)

您可以将AVPlayer添加为图层,并添加自己的自定义按钮

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