ios swift播放从cloudkit上传的视频

时间:2016-05-08 04:51:53

标签: ios swift

我正在尝试播放从cloudkit下载的视频。我使用与下载图像相同的查询方法:

publicData.performQuery(query, inZoneWithID: nil) { results, error in
    if error == nil { // There is no error
        for cafe in results! {
            let newCafe = Cafe()
            newCafe.address = cafe["address"] as? String
            newCafe.name = cafe["name"] as? String
            newCafe.email = cafe["email"] as? String
            newCafe.description = cafe["description"] as? String
            newCafe.location = cafe["location"] as? CLLocation
            newCafe.cafeImage = cafe["cafeImage"] as? CKAsset
            newCafe.offer_wifi = cafe["offer_wifi"] as? Bool
            newCafe.smoking_area = cafe["smoking_area"] as? Bool
            newCafe.cafeVideo = cafe["video"] as? CKAsset // <== I want to use this
            self.cafes.append(newCafe)
            let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()

            defaults.setInteger(self.cafes.count, forKey: "PreviousCafeCount")


            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.tableView.reloadData()
                Spinner.sharedLoader.hideLoading()
            })

在cafeDetailViewController中,我创建了一个按钮,触发使用AVPlayer播放视频。 AVKit和AVFoundation已经导入。

@IBAction func playVideo(sender: AnyObject) {
    if let file = cafeDetail.cafeVideo {
        let player = AVPlayer(URL: file.fileURL)
        let playerController = AVPlayerViewController()

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

        player.play()
    }
}

但结果如下:enter image description here

跟进问题:如何在swift中实现模型关联?类似于rails中的has_many和belongs_to关联。我不认为事先下载整个视频是一个很好的解决方案。

1 个答案:

答案 0 :(得分:0)

从我所看到的,您需要将视频保存到本地文件,然后播放该文件。这是从我写的东西改为使用CloudKit。

    import UIKit
    import CloudKit
    import AVKit
    import AVFoundation

    class CloudKitTestViewController: UIViewController {

    let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase

    var videoURL: NSURL!

    @IBAction func load(sender: AnyObject) {

        let predicate = NSPredicate(format: "videoName = %@", "nameOfVideoGoesHere")

        activityIndicator.startAnimating()

        let query = CKQuery(recordType: "Videos", predicate: predicate)

        publicDatabase.performQuery(query, inZoneWithID: nil) { (results, error) in
            if error != nil {
                dispatch_async(dispatch_get_main_queue()) {
                    self.notifyUser("Cloud Access Error",
                                    message: error!.localizedDescription)
                }
            } else {
                if results!.count > 0 {
                    let record = results![0]

                    dispatch_async(dispatch_get_main_queue()) {
                        !)

                        let video = record.objectForKey("videoVideo") as! CKAsset

                        self.videoURL = video.fileURL 

                        let videoData = NSData(contentsOfURL: self.videoURL!)

                        let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
                        let destinationPath = NSURL(fileURLWithPath: documentsPath).URLByAppendingPathComponent("filename.mov", isDirectory: false)

                        NSFileManager.defaultManager().createFileAtPath(destinationPath.path!, contents:videoData, attributes:nil)

                        self.videoURL = destinationPath

                        print(self.videoURL)

                    }
                } else {
                    dispatch_async(dispatch_get_main_queue()) {
                        self.notifyUser("No Match Found",
                                        message: "No record matching the address was found")
                    }
                }
            }

            dispatch_async(dispatch_get_main_queue(), {
                self.activityIndicator.stopAnimating()
            })
        }


    }

    override func prepareForSegue(segue: UIStoryboardSegue,
                                  sender: AnyObject?) {
        let destination = segue.destinationViewController as!
        AVPlayerViewController
        let url = videoURL
        print(videoURL)
        destination.player = AVPlayer(URL: url!)
    }

}