AssetDownloadWrapper
使用AVAssetDownloadURLSession下载视频的包装器。
关于
这是一个包装了AVAssetDownloadURLSession功能的库。下载视频并将其缓存到本地存储中。
如何实现
- 下载视频
let url = URL(string: "https://mnmedias.api.telequebec.tv/m3u8/29880.m3u8")!
let urlAsset = AVURLAsset(url: url)
let asset = AssetWrapper(urlAsset: urlAsset, assetTitle: "hoge")
AssetDownloadManager.shared.downloadStream(for: asset) { (result) in
switch result {
case .success(let response):
print(response)
case .failure(let error):
print(error)
}
}
- 加载本地缓存
guard let arg = AssetDownloadManager.shared.retrieveLocalAsset(with: "hoge") else {
return
}
let vc = AVPlayerViewController()
let item = AVPlayerItem(asset: arg.0.urlAsset)
player.replaceCurrentItem(with: item)
vc.player = player
present(vc, animated: true, completion: nil)
需求
- Xcode 11.4
- Swift 5.1
安装
CocoaPods
添加 pod AssetDownloadWrapper
# Podfile
use_frameworks!
target 'YOUR_TARGET_NAME' do
pod 'AssetDownloadWrapper'
end
$ pod install
Carthage
将其添加到 Cartfile 中
git "[email protected]:t-osawa-009/AssetDownloadWrapper.git"
$ carthage update
参考资源
- https://blog.foresta.me/posts/swift_hls_download_bug/
- https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/using_avfoundation_to_play_and_persist_http_live_streams
- https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/MediaPlaybackGuide/Contents/Resources/en.lproj/HTTPLiveStreaming/HTTPLiveStreaming.html#//apple_ref/doc/uid/TP40016757-CH11-SW1
贡献
这里还有很多工作要做。我们非常愿意看到您的参与。您可以在 贡献指南 中找到如何开始的全部细节。
许可证
AssetDownloadWrapper 依据 MIT 许可证发布。有关详细信息,请参阅 LICENSE 文件。