无法使用Swift使用AVPlayer播放视频

时间:2016-02-22 23:53:55

标签: swift video-streaming nsurl avplayerviewcontroller avkit

当我点击播放视频时,它会显示如下。想知道为什么。没有显示错误。我确实导入了AVKit和AVFoundation。另外,我在我的设备和模拟器中测试过。仍然无法正常工作。有什么建议?非常感谢。

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

1 个答案:

答案 0 :(得分:1)

您是否看过并阅读了有关此文档的文档...

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

您确定将数据保存为电影而不是图像吗?疯狂猜测?

我好像回忆起一篇新文章,说parse.com正在关机,所以也许使用它也不是一个好主意......

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