iOS离线HLS文件大小 [英] iOS offline HLS file size
问题描述
在iOS 10中,Apple添加了离线HLS.他们在文档中提到:
In iOS 10, Apple added offline HLS. In the documentation, they mention:
重要提示:已下载的HLS资产以私有方式存储在磁盘上 捆绑格式.该捆绑包格式可能会随着时间的推移而变化,开发人员 不应尝试访问或存储捆绑包中的文件 直接使用,而应使用AVFoundation和其他iOS API来 与下载的资产进行交互.
Important: Downloaded HLS assets are stored on disk in a private bundle format. This bundle format may change over time, and developers should not attempt to access or store files within the bundle directly, but should instead use AVFoundation and other iOS APIs to interact with downloaded assets.
似乎对这些文件的信息的访问受到限制.我正在尝试查找存储文件的大小.这是我的工作.下载完成后,我保存了相对路径
It appears the access to information about these files is limited. I'm trying to find the size of the stored file. Here is what I do. After download finishes, I save the relative path
func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
//Save path
video?.downloadPath = location.relativePath
}
后来我按如下方式重建文件路径
later I reconstruct the file path as follows
if let assetPath = workout.downloadPath {
let baseURL = URL(fileURLWithPath: NSHomeDirectory())
let assetURL = baseURL.appendingPathComponent(assetPath)
这有效:
try FileManager.default.removeItem(at: assetURL)
这不存在,并返回文件不存在的错误:
This does not and returns an error that the file doesn't exist:
let att = try FileManager.default.attributesOfItem(atPath: assetURL.absoluteString)
我可以按以下方式加载视频资产,并通过以下方式离线播放:
I can load in the video asset as follows and play it offline with:
let avAsset = AVURLAsset(url: assetURL)
但这会返回一个空数组:
But this returns me an empty array:
let tracks = avAsset.tracks(withMediaType: AVMediaTypeVideo)
我再次尝试获取离线HLS资产的文件大小.在SO上使用FileManager获取文件大小的其他答案似乎不适用于这些,也无法获取已加载的AVAset的大小的答案.预先感谢.
Once again I'm just trying to get the file size of an offline HLS asset. It appears the other answers on SO for getting a file size using FileManager don't work for these nor do the answers for getting the size of a loaded AVAsset. Thanks in advance.
推荐答案
唯一的方法是对存储下载内容的文件夹中的所有文件大小求和.
The only way is to sum all files sizes inside a folder where your downloaded content is stored.
- (NSUInteger)hlsFileSize:(NSURL *)fileURL {
NSUInteger size = 0;
let enumerator = [NSFileManager.defaultManager enumeratorAtURL:fileURL includingPropertiesForKeys:nil options:0 errorHandler:nil];
for (NSURL *url in enumerator) {
NSError *error = nil;
// Get values
let resourceValues = [url resourceValuesForKeys:@[NSURLIsRegularFileKey, NSURLFileAllocatedSizeKey, NSURLNameKey] error:&error];
// Skip unregular files
let isRegularFile = [resourceValues[NSURLIsRegularFileKey] boolValue];
if (!isRegularFile) {
continue;
}
let fileAllocatedSize = [resourceValues[NSURLFileAllocatedSizeKey] unsignedLongLongValue];
size += fileAllocatedSize;
}
return size;
}
这篇关于iOS离线HLS文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!