gpt4 book ai didi

ios - 为什么我找不到使用 AVPlayerViewController 播放视频的方法

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

大家好

我被这个问题困扰了一整天,实际上我没有找到播放视频文件的方法,我将解释一下我所做的:

我有以下网址:[https://rapidresponse-us-oem-storage11.mios.com/storage/storage/store/8157061/archive?Key=833101823][1]

如果您可以在该网址中看到我正在获取一个视频文件 .mp4 并且我需要播放该视频,以下是我正在使用的代码,首先我要保存该文件,然后我'我尝试在 AVPlayerViewController 中播放:

let store = self.eventDetail?.EventDetailStore!
let url = "https://" + (self.eventDetail?.EventDetailServerStorage)! + "/storage/storage/store/" + store! + "/archive?Key=" + self.EventKey!
if let videoUrl = NSURL(string: url) {
// create your document folder url
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL
// your destination file url
let destinationUrl = documentsUrl.URLByAppendingPathComponent(videoUrl.lastPathComponent!)
print(destinationUrl)
// check if it exists before downloading it
if NSFileManager().fileExistsAtPath(destinationUrl.path!) {
print("The file already exists at path")
let player = AVPlayer(URL: destinationUrl)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}

} else {
// if the file doesn't exist
// just download the data from your url
if let myAudioDataFromUrl = NSData(contentsOfURL: videoUrl){
// after downloading your data you need to save it to your destination url
if myAudioDataFromUrl.writeToURL(destinationUrl, atomically: true) {
print(destinationUrl)
let player = AVPlayer(URL: destinationUrl)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}

} else {
let alert = UIAlertController(title: "Alert", message: "An error occurred", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
}
}

当我运行代码时,我得到视频 Controller ,但不会重现任何类似不存在文件的东西,但我调试代码并且文件似乎已保存。

如果您能帮助解决此问题,我将不胜感激

根据您的建议,我更新了代码,但尚未工作:

//video
var player: AVPlayer!
var destinationUrl: NSURL? = nil
dispatch_async(dispatch_get_global_queue(priority, 0)) {
let store = self.eventDetail?.EventDetailStore!
let url = "https://" + (self.eventDetail?.EventDetailServerStorage)! + "/storage/storage/store/" + store! + "/archive?Key=" + self.EventKey!
if let videoUrl = NSURL(string: url) {
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL
// your destination file url
destinationUrl = documentsUrl.URLByAppendingPathComponent(videoUrl.lastPathComponent!)
if NSFileManager().fileExistsAtPath(destinationUrl!.path!) == false {
if let myAudioDataFromUrl = NSData(contentsOfURL: videoUrl){
myAudioDataFromUrl.writeToURL(destinationUrl!, atomically: true)
}
}
}
dispatch_async(dispatch_get_main_queue()) {
if(destinationUrl != nil){
player = AVPlayer(URL: destinationUrl!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true, completion: { () -> Void in playerViewController.player!.play() })
}
}
}
}

最佳答案

您的播放器超出了范围,并且在视频播放完毕之前被取消分配。将其分配给类成员以延长其生命周期。

关于ios - 为什么我找不到使用 AVPlayerViewController 播放视频的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33353709/

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