从iOS 10开始,苹果就提供了下载HLS (m3u8)视频供离线浏览的支持。
我的问题是:我们是否有必要在播放时才能下载HLS?或者,当用户按下下载按钮并显示进度时,我们可以直接下载。
是否有人在目标C版本中实现了这一点?实际上,我以前的应用程序是在目标C中创建的。现在,我想添加对下载HLS的支持,而不是MP4 (以前我下载MP4用于脱机视图)。
我真的很想这么做。如果已实现,请分享想法或任何代码。
发布于 2016-11-14 14:31:05
唯一能做到这一点的方法是在下载完文件后,设置一个HTTP服务器,在本地服务这些文件。
实况播放列表使用滑动窗口。您需要在目标持续时间之后定期重新加载它,并只下载列表中出现的新段(它们将在稍后被删除)。
以下是一些相关的答案: IOS设备可以使用m3u8视频和phonegap/cordova从本地文件系统中流html5分段视频吗?
发布于 2019-02-02 12:48:18
我使用apple代码guid下载HLS内容,代码如下:
var configuration: URLSessionConfiguration?
var downloadSession: AVAssetDownloadURLSession?
var downloadIdentifier = "\(Bundle.main.bundleIdentifier!).background"
func setupAssetDownload(videoUrl: String) {
// Create new background session configuration.
configuration = URLSessionConfiguration.background(withIdentifier: downloadIdentifier)
// Create a new AVAssetDownloadURLSession with background configuration, delegate, and queue
downloadSession = AVAssetDownloadURLSession(configuration: configuration!,
assetDownloadDelegate: self,
delegateQueue: OperationQueue.main)
if let url = URL(string: videoUrl){
let asset = AVURLAsset(url: url)
// Create new AVAssetDownloadTask for the desired asset
let downloadTask = downloadSession?.makeAssetDownloadTask(asset: asset,
assetTitle: "Some Title",
assetArtworkData: nil,
options: nil)
// Start task and begin download
downloadTask?.resume()
}
}//end method
func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
// Do not move the asset from the download location
UserDefaults.standard.set(location.relativePath, forKey: "testVideoPath")
}如果你不明白发生了什么,在这里读一读:https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/MediaPlaybackGuide/Contents/Resources/en.lproj/HTTPLiveStreaming/HTTPLiveStreaming.html
现在,您可以使用存储的HSL内容在AVPlayer中使用以下代码播放视频:
//get the saved link from the user defaults
let savedLink = UserDefaults.standard.string(forKey: "testVideoPath")
let baseUrl = URL(fileURLWithPath: NSHomeDirectory()) //app's home directory
let assetUrl = baseUrl.appendingPathComponent(savedLink!) //append the saved link to home path现在使用路径在AVPlayer中播放视频
let avAssest = AVAsset(url: assetUrl)
let playerItem = AVPlayerItem(asset: avAssest)
let player = AVPlayer(playerItem: playerItem) // video path coming from above function
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true, completion: {
player.play()
})发布于 2017-06-12 12:24:30
您可以使用AVAssetDownloadURLSession makeAssetDownloadTask轻松下载HLS流。看看Apples代码中的AssetPersistenceManager:https://developer.apple.com/library/content/samplecode/HLSCatalog/Introduction/Intro.html,使用api的目标C版本应该是相当直接的。
https://stackoverflow.com/questions/40424263
复制相似问题