NSP*_*tik 5 http-live-streaming ios avplayer m3u8 avassetdownloadtask
从iOS 10开始,Apple提供了下载HLS(m3u8)视频以供离线查看的支持。
我的问题是:是否只有在播放HLS时才可以下载?或者我们可以在用户按下下载按钮并显示进度时下载。
是否有人在Objective C版本中实现了此功能?实际上,我以前的应用程序是在Objective C中制作的。现在,我想增加对下载HLS而不是MP4的支持(以前我是为了离线查看而下载MP4的)。
我真的很绝望。请分享想法或任何实施的代码。
I used the apple code guid to download HLS content with the following code:
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")
}
Run Code Online (Sandbox Code Playgroud)
if you don't understand what's going on, read up about it here: https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/MediaPlaybackGuide/Contents/Resources/en.lproj/HTTPLiveStreaming/HTTPLiveStreaming.html
now you can use the stored HSL content to play the video in AVPlayer with the following code:
//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
Run Code Online (Sandbox Code Playgroud)
now use the path to play video in 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()
})
Run Code Online (Sandbox Code Playgroud)
执行此操作的唯一方法是设置一个 HTTP 服务器,以便在下载文件后在本地提供文件服务。
现场播放列表使用滑动窗口。您需要在目标持续时间后定期重新加载它,并仅下载列表中出现的新段(它们将在稍后删除)。
以下是一些相关答案:Can IOS devices Streaming m3u8分段视频从本地文件系统使用html5视频和phonegap/cordova?
| 归档时间: |
|
| 查看次数: |
3616 次 |
| 最近记录: |