Apple tvOS 中的视频下载
Video Download in Apple tvOS
我是 swift 的新手,tvOS.I 想从 url 下载视频并在 ViewController 中显示。当我尝试使用 ios 设备时,视频已下载并可在 tvOS 上运行 fine.But,但视频未下载。
为什么会这样..?
如何在 Apple TV 中下载和播放视频。
这是我的代码
让 backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: "backgroundSession")
backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)
let url = URL(string: "myURl")!
downloadTask = backgroundSession.downloadTask(with: url)
downloadTask.resume()
func urlSession(_ session: URLSession,
下载任务:URLSessionDownloadTask,
didFinishDownloadingTo 位置:URL){
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentDirectoryPath:String = path[0]
let fileManager = FileManager()
let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat("/file.mp4"))
if fileManager.fileExists(atPath: destinationURLForFile.path){
showFileWithPath(path: destinationURLForFile.path)
}
else{
do {
try fileManager.moveItem(at: location, to: destinationURLForFile)
// show file
showFileWithPath(path: destinationURLForFile.path)
}catch{
print("An error occurred while moving file to destination url")
}
}
}
tvOS 在本地存储方面有一些限制
Your app can only access 500 KB of persistent storage that is local to the device (using the NSUserDefaults class). Outside of this limited local storage, all other data must be purgeable by the operating system when space is low...
您可以查看此文档以了解更多详细信息:
https://developer.apple.com/library/content/documentation/General/Conceptual/AppleTV_PG/
我是 swift 的新手,tvOS.I 想从 url 下载视频并在 ViewController 中显示。当我尝试使用 ios 设备时,视频已下载并可在 tvOS 上运行 fine.But,但视频未下载。
为什么会这样..?
如何在 Apple TV 中下载和播放视频。
这是我的代码
让 backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: "backgroundSession")
backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)
let url = URL(string: "myURl")!
downloadTask = backgroundSession.downloadTask(with: url)
downloadTask.resume()
func urlSession(_ session: URLSession, 下载任务:URLSessionDownloadTask, didFinishDownloadingTo 位置:URL){
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentDirectoryPath:String = path[0]
let fileManager = FileManager()
let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat("/file.mp4"))
if fileManager.fileExists(atPath: destinationURLForFile.path){
showFileWithPath(path: destinationURLForFile.path)
}
else{
do {
try fileManager.moveItem(at: location, to: destinationURLForFile)
// show file
showFileWithPath(path: destinationURLForFile.path)
}catch{
print("An error occurred while moving file to destination url")
}
}
}
tvOS 在本地存储方面有一些限制
Your app can only access 500 KB of persistent storage that is local to the device (using the NSUserDefaults class). Outside of this limited local storage, all other data must be purgeable by the operating system when space is low...
您可以查看此文档以了解更多详细信息: https://developer.apple.com/library/content/documentation/General/Conceptual/AppleTV_PG/