gpt4 book ai didi

ios 快速播放从cloudkit上传的视频

转载 作者:行者123 更新时间:2023-11-30 13:26:42 25 4
gpt4 key购买 nike

我正在尝试播放从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关联。我认为提前下载整个视频不是一个好的解决方案。

最佳答案

据我所知,您需要将视频保存到本地文件,然后播放该文件。这是我为了搞乱 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!)
}

}

关于ios 快速播放从cloudkit上传的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37096334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com