AVPlayerController - 按钮无法全屏工作
AVPlayerController - Buttons not working on full screen
我正在尝试了解 AVPlayer 和 AVPlayerController 的工作原理。
我将我的播放器嵌入到一个视图中,所以我的播放器有两种模式:小屏幕和全屏。
当它很小时,play/plause 按钮和全屏按钮工作得很好。但是一旦全屏,以下按钮将不起作用:播放、暂停、完成、最小化。
这是我的代码:
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 种方法的委托吗?
它对我有用 self.present(playerController, animated: true, completion:nil)
既然是控制器,我们就应该把它当作控制器来使用,而不是作为子视图来使用。
您可以将 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];
self.addChildController(playerController);
我已将它添加到您的主视图控制器上,它可以为 "Done" 和 "Minimise" 提供帮助。
我正在尝试了解 AVPlayer 和 AVPlayerController 的工作原理。 我将我的播放器嵌入到一个视图中,所以我的播放器有两种模式:小屏幕和全屏。 当它很小时,play/plause 按钮和全屏按钮工作得很好。但是一旦全屏,以下按钮将不起作用:播放、暂停、完成、最小化。
这是我的代码:
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 种方法的委托吗?
它对我有用 self.present(playerController, animated: true, completion:nil)
既然是控制器,我们就应该把它当作控制器来使用,而不是作为子视图来使用。
您可以将 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];
self.addChildController(playerController);
我已将它添加到您的主视图控制器上,它可以为 "Done" 和 "Minimise" 提供帮助。