2016-11-25 74 views
1

我想了解AVPlayer & AVPlayerController的工作原理。 我在视图中嵌入了我的播放器,所以我的播放器有两种模式:小屏幕&全屏。 当播放/播放按钮和全屏按钮很少播放时很好。但一旦它是全屏幕,下面的按钮不起作用:播放,暂停,完成,最小化。AVPlayerController - 按钮无法在全屏幕上工作

这里是我的代码:

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种方法的代表?

回答

1
self.addChildController(playerController); 

我已经将它添加你的主视图控制器,它做“完成”和“最小化”的技巧。

0

它为我的self.present(的PlayerController,动画:真,完成:无)

既然是控制器,我们应该把它作为一个控制器,而不是子观看它。

+0

我不想出示它,我希望它被嵌入到视图中... –

0

您可以添加AVPlayer作为一个层,并添加自己的自定义按钮

球员= [[[AVPlayer页头]初始化...

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

有没有办法使用已经在这里的人? –