当应用程序在tvOS视频播放期间进入前台时,前进和后退功能无效

时间:2016-06-29 13:17:02

标签: ios swift avplayer tvos avplayerviewcontroller

我正在使用swift开发tvOS应用程序。我正在使用AVPlayerViewController在Apple演示应用程序(UIKit目录(tvOS):创建和自定义UIKit控件)之后播放视频。一切都运行良好,但应用程序在视频播放期间进入前景背景,然后前进和后退不工作,如果用户可以转到睡眠模式,则会产生同样的问题。此问题也在Apple演示应用程序中生成。

我不明白为什么会出现这个问题。请建议我如何解决它。

    import UIKit
    import Foundation
    import AVKit

    class ViewController: UIViewController ,AVPlayerViewControllerDelegate{
        let playerController = AVPlayerViewController()
        var playerObj:AVPlayer!
        var asset:AVAsset!
        var playerItem:AVPlayerItem!
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
         self.playVideo("http://p.events-delivery.apple.com.edgesuite.net/1509pijnedfvopihbefvpijlkjb/m3u8/hls_vod_mvp.m3u8")
        }
  override func viewWillAppear(animated: Bool) {
         playerController.delegate = self
    }
        //MARK:- Play Video Method.
        func playVideo(videoURL:String) {
            print("video URL========\(videoURL)")

            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
            playerController.view.frame = CGRectMake(0.0, 0.0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)
            playerController.delegate = self
            self.view.addSubview(playerController.view)
            self.playerController.showsPlaybackControls = true
            self.playerItem.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.New, context: nil)
            self.playerController.player!.play()
            //self.playerController.player!.play()


        }
        override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>){

            if object as? NSObject == playerItem && (keyPath! as NSString).isEqualToString("status") {

                if (playerItem?.status == AVPlayerItemStatus.ReadyToPlay) {
                      print("Player Ready to play")
                }
                if (playerItem?.status == AVPlayerItemStatus.Failed) || (playerItem?.status == AVPlayerItemStatus.Unknown) {

                        print("Player getting play error")
                }
            }
        }


        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }


    }

1 个答案:

答案 0 :(得分:0)

尝试一下......可能不是&#34;最好的&#34;解决方案但它显示了我在谈论视图控制器生命周期时所说的内容。

旁注,我建议您始终在视图控制器的重写函数中调用super.viewWillAppear(动画)。

import UIKit
import Foundation
import AVKit
import AVFoundation

class ViewController: UIViewController ,AVPlayerViewControllerDelegate{
    let playerController = AVPlayerViewController()
    var playerObj:AVPlayer = AVPlayer()
    var asset:AVAsset!
    var playerItem:AVPlayerItem!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated) // always call this
    playerController.delegate = self
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(setDelegateOnForground), name: "application_forgrounded", object: nil)
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated) // always call this
    // check the player rate if 0 means it's paused / not playing

如果playerObj.rate == 0 {  的playVideo(&#34; http://p.events-delivery.apple.com.edgesuite.net/1509pijnedfvopihbefvpijlkjb/m3u8/hls_vod_mvp.m3u8&#34)     }     }     // MARK: - 播放视频方法。     func playVideo(videoURL:String){         打印(&#34;视频网址========(videoURL)&#34;)         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         playerController.view.frame = CGRectMake(0.0,0.0,UIScreen.mainScreen()。bounds.width,UIScreen.mainScreen()。bounds.height)         self.view.addSubview(playerController.view)         self.playerController.showsPlaybackControls = true         self.playerItem.addObserver(self,forKeyPath:&#34; status&#34;,options:NSKeyValueObservingOptions.New,context:nil)         self.playerController.player!.play()         //self.playerController.player!.play()

}

func setDelegateOnForground() {
    playerController.delegate = self
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>){

    if object as? NSObject == playerItem && (keyPath! as NSString).isEqualToString("status") {

        if (playerItem?.status == AVPlayerItemStatus.ReadyToPlay) {
            print("Player Ready to play")
        }
        if (playerItem?.status == AVPlayerItemStatus.Failed) || (playerItem?.status == AVPlayerItemStatus.Unknown) {

            print("Player getting play error")
        }
    }
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

中的AppDelegate.swift中的

    func applicationDidBecomeActive(application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    NSNotificationCenter.defaultCenter().postNotification(NSNotification(name: "application_forgrounded", object: nil))
}

这将保证设置您的代理