AVPlayer暂停不工作TVOS

时间:2015-11-12 10:21:04

标签: ios video-streaming avplayer tvos avplayerviewcontroller

我正在制作一个tvOS应用程序。我的应用程序中有两个标签AB。假设视频正在VC中的标签A中播放。当我从A->B切换标签时,我想暂停当前正在播放的视频。我在viewWillDisappear中这样做,但视频第一次暂停。当我再次切换标签B->A并点击电视遥控器上的播放按钮时,它会播放,但如果我再次尝试切换标签A->B,则视频会继续播放并且不会停止播放。

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)


        avplayerVC?.player?.rate = 0.0
        print("Player Playing Rate : ", "\(avplayerVC?.player?.rate)")
    }

我正在使用avPlayer.pause()方法和avPlayer.rate = 0.0来阻止它,但每次都没有。

日志显示Player Playing Rate : Optional(0.0)。但视频实际上并没有暂停。这是我的完整代码。

    class PlayViewController : UIViewController {


        var avplayerVC : AVPlayerViewController?
        var videoUrlStr : String?

        override func viewDidLoad() {
            super.viewDidLoad()

            print("In PlayViewController View Did Load")

            avplayerVC = AVPlayerViewController()

            let avAsset = AVAsset(URL: NSURL.init(string: videoUrlStr!)!)
            let avPlayerItem = AVPlayerItem(asset: avAsset)
            avplayerVC?.player = AVPlayer(playerItem: avPlayerItem)

            avplayerVC?.player?.seekToTime(kCMTimeZero)
            print("Status 1: " +  "\(avplayerVC?.player?.status.rawValue)")

            print(self.view?.frame)

            // doesn't work
            avplayerVC?.view.frame =  self.view.frame


            self.view.addSubview((avplayerVC?.view!)!)
            avplayerVC?.player?.play()
        }


        override func viewWillDisappear(animated: Bool) {
            super.viewWillDisappear(animated)

            print("Changing rate")
            avplayerVC?.player?.rate = 0.0
            print("Player Playing Rate : ", "\(avplayerVC?.player?.rate)")
        }
   }

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

AVPlayerViewController我遇到了同样的问题,也许我的回答可以帮到你:here