1

//Firstly my English very bad, I'm sorry... I'm implementing AVPlayer my app. Player is working fine but if I play another videos many times Player is not working and only just this screen looks.

Here's my code

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 Answers1

0

I don't understand the way you fetch the video object but you can try the following code. It works fine.

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()

    }

}
aytek
  • 1,842
  • 24
  • 32
  • I'm fetching the PFQueryTableViewController. This Controller Detail View Controller. Problem is not fetching. AVPlayer already work fine and playing videos. My problem is I play videos many many times and after a while AVPlayer not working fine. – Barış Çiçek Jul 15 '16 at 17:09
  • Check [this](http://stackoverflow.com/questions/19291636/avplayer-stops-playing-and-dont-resume-again) – aytek Jul 16 '16 at 11:34