我可以播放一个捆绑提供的.mpg / .mp4文件;但不是远程URL。补救?

时间:2015-10-01 05:59:04

标签: video-streaming swift2 avplayer

我试图通过其网址播放视频;即视频流。
当URL在本地时,以下代码在捆绑中起作用:

class EditShowVideoViewController:AVPlayerViewController {

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    override func viewDidLoad() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleNotification",
            name:AVPlayerItemDidPlayToEndTimeNotification,
            object: nil)

        if let url = NSURL(string: gEditMediumTuple!.medium as! String) {
            player = AVPlayer(URL: url)
            player!.play()
        } else {
            showAlert(sender: self.parentViewController!, withTitle: "No Video", withMessage: "No video is found.", alertPurpose: .noVideo)
        }
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.AllButUpsideDown
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    func handleNotification() {
        let controller = self.parentViewController as! EditContainerViewController
        controller.rollback()
    }
}

但它不适用于远程URL。所以我尝试了以下方法:

let playerItem = AVPlayerItem(URL:url)
player = AVPlayer(playerItem:playerItem)
player!.play()

但我总是得到以下结论:
enter image description here

我假设我必须在播放之前等待.mpg加载。
什么是首选方式?

1 个答案:

答案 0 :(得分:0)

这对我有用

let playerVC = AVPlayerViewController()
let url = NSURL(string: "http://urltofile")!
let player = AVPlayer(URL: url)
playerVC.player = player
self.addChildViewController(playerVC)
self.view.addSubview(playerVC.view)
playerVC.view.frame = self.view.frame
player.play()
小心:如果您使用的是iOS 9,ATS可能会阻止您的http呼叫