AVPlayerViewController播放/暂停无法在tvOS

时间:2016-06-17 07:31:59

标签: tvos apple-tv avplayerviewcontroller tvos9.1

我正在研究tvOS app。我有一个视频列表,在播放主视频之前需要播放具有跳过功能的DFP广告。 我在AVPlayerViewController的视图中添加了skip按钮并管理焦点(将焦点跳过按钮移动到AVPlayerViewController的视图)。如果我将AVPlayerViewController添加为childViewController但主视频播放/暂停功能不起作用,焦点工作正常。

self.addChildViewController(playerController)
self.view.addSubview(playerController.view)

如果我已将AVPlayerViewController添加为ParentViewController,则焦点无效。

 self.view.addSubview(playerController.view)
 playerController.didMoveToParentViewController(self)

我已经实施了打击代码

let playerController = AVPlayerViewController()
var playerObj:AVPlayer!
var asset:AVAsset!
var playerItem:AVPlayerItem!


override func viewDidLoad() {
    super.viewDidLoad()

    playerController.view.frame = CGRectMake(0.0, 0.0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)
    self.addChildViewController(playerController)
    self.view.addSubview(playerController.view)
    self.preferredFocusedView
    self.updateFocusIfNeeded()
}
func playVideo(videoURL:String) {
    self.playerController.showsPlaybackControls = false
    self.asset = AVAsset(URL: NSURL(string: videoURL)!) as AVAsset
    self.playerItem = AVPlayerItem(asset: self.asset)
    self.playerObj = AVPlayer(playerItem: self.playerItem)
    self.playerController.player = self.playerObj
    self.playerItem.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.New, context: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(VideoPlayerViewController.playerItemDidReachEnd(_:)), name:    AVPlayerItemDidPlayToEndTimeNotification, object: self.playerObj.currentItem)
    if !self.controlFlag{
        // For main video
        playFlag = true
        self.playerController.showsPlaybackControls = true

    }else{
     // for DFP ad
         playFlag = false
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(VideoPlayerViewController.playerStalledNotification(_:)), name: AVPlayerItemPlaybackStalledNotification, object: self.playerObj.currentItem)
    }
    self.playerController.player!.play()
}

 override var preferredFocusedView: UIView? {
    get {
        if skipAdsButton != nil {

            print("focus on skip button")
            return self.skipAdsButton
        }else{
             print("focus on super.preferredFocusedView")
            return super.preferredFocusedView
        }
    }
}

如何解决此问题。请提出建议。

0 个答案:

没有答案