Search code examples
swiftvideo-streamingnsurlavplayerviewcontrolleravkit

Unable to play video using AVPlayer with Swift


When I click to play video, it shows like this. Wondering why. No error is shown. I did import AVKit and AVFoundation. Also, I tested in my device and simulator. Still does not work. Any suggestions? Thanks a lot.

enter image description here

**Print log**
    //<PFFile: 0x79d472f0>
    //https://files.parsetfss.com/d9cdffff-4a1f-4bd9-b6ee-e60c07d49236/tfss-7e1636b9-5701-4737-9fa6-10609aec89e8-adsVido
    //<AVPlayer: 0x786f39b0>

// Click cell to play video.
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        if adsVideoArray[indexPath.row] != nil {
            let theVideo:PFFile = adsVideoArray[indexPath.row]!
            print(theVideo)
            let url:NSURL = NSURL(string: theVideo.url!)!
            theVideo.getDataInBackgroundWithBlock({ (data:NSData?, error: NSError?) -> Void in
                if error == nil {
                    let player = AVPlayer(URL: url)
                    print(url)
                    print(player)
                    let playerController = AVPlayerViewController()
                    playerController.player = player
                    self.presentViewController(playerController, animated: true) {
                        player.play()
                    }
                }
            })
        }
    }

// load data from parse
    func loadAdsFromParse() {
        adsVideoArray.removeAll(keepCapacity: false)
        let query = PFQuery(className: "user_ads")
        query.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
            for object in objects! {
                self.adsVideoArray.append(object.objectForKey("video") as? PFFile)
            }
            dispatch_async(dispatch_get_main_queue()) {
                self.adsCollectionView.reloadData()
            }
        })
    }

// the way to save video
    self.tempVideo = info[UIImagePickerControllerMediaURL] as! NSURL!
    let videoData = NSData(contentsOfFile:self.tempVideo.relativePath!)
    let videoFile:PFFile = PFFile(name:"adsVido", data:videoData!)!
    detailsObj["video"] = videoFile
    detailsObj.saveInBackgroundWithBlock{ ... }

// I tried to play the video before upload it to Parse. It works.
func playImageTapped(pressed: UIGestureRecognizer) {
    let player = AVPlayer(URL: tempVideo)
    let playerController = AVPlayerViewController()
    playerController.player = player
    self.presentViewController(playerController, animated: true) {
        player.play()
    }
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let mediaType = info[UIImagePickerControllerMediaType] as! NSString
    self.tempVideo = videoAdded

Solution

  • Did you look and read the documentation on this...

    https://parse.com/docs/osx/api/Classes/PFFile.html

    Are you sure your saving your data as a movie and not perhaps as an image? a wild guess?

    I seem to recall a new article saying parse.com is being shutdown, so maybe it not a good idea to use it anyway...

    http://blog.parse.com/announcements/moving-on/