AVPlayer无法正常工作 - Swift

时间:2016-07-15 16:07:13

标签: swift parse-platform video-streaming avplayer avplayerviewcontroller

//首先我的英语非常糟糕,我很抱歉...... 我正在实施AVPlayer我的应用程序。播放器工作正常,但如果我多次播放另一个视频播放器不工作,只有http://localhost/myserver/whatever看起来。

这是我的代码

let player:AVPlayer?

override func viewDidLoad() {
    super.viewDidLoad()

    let videoFile = object?.objectForKey("video") as? PFFile

    videoFile?.getFilePathInBackgroundWithBlock({ (filePath, error) in
        if error == nil {
            let videoURL = NSURL(fileURLWithPath: filePath!)

            self.player = AVPlayer(URL: videoURL)
            let playerController = AVPlayerViewController()
            playerController.player = self.player

            playerController.showsPlaybackControls = false
            playerController.videoGravity = AVLayerVideoGravityResizeAspectFill

            playerController.view.frame = self.videoView.bounds
            self.videoView.addSubview(playerController.view)
            self.addChildViewController(playerController)

            self.player!.play()

        } else {
            print(error)
        }
    })


}

1 个答案:

答案 0 :(得分:0)

我不了解您获取视频对象的方式,但您可以尝试以下代码。它工作正常。

var player:AVPlayer! // this shouldn't be let.

override func viewDidLoad() {
    super.viewDidLoad()

    let query = PFQuery(className:"TestClass") //your classname in parse. Change it with your clas name
    query.getObjectInBackgroundWithId("SALe4gX2nk") { // get the video file with id. Change it with your id.
    (object: PFObject?, error: NSError?) -> Void in
        let videoFile = object?.objectForKey("video") as? PFFile
        let url = NSURL(string: (videoFile?.url)!)

        self.player = AVPlayer(URL: url!)
        let playerController = AVPlayerViewController()
        playerController.player = self.player

        playerController.showsPlaybackControls = false
        playerController.videoGravity = AVLayerVideoGravityResizeAspectFill

        playerController.view.frame = self.view.bounds
        self.view.addSubview(playerController.view)
        self.addChildViewController(playerController)

        self.player!.play()

    }

}