下载和播放离线 HLS 内容 - iOS 10

Downloading and playing offline HLS Content - iOS 10

从iOS10开始,Apple提供了下载HLS(m3u8)视频供离线观看的支持。

我的问题是:HLS是不是一定要在播放的时候才能下载?或者我们可以在用户按下下载按钮并显示进度时下载。

有人在 Objective C 版本中实现过这个吗?其实我之前的App是Objective C做的。现在我想添加对下载 HLS 而不是 MP4 的支持(之前我是下载 MP4 以供离线查看)。

我真的很绝望。如果实施,请分享想法或任何代码。

执行此操作的唯一方法是设置一个 HTTP 服务器,以便在下载文件后在本地提供这些文件。

现场播放列表使用滑动-window。您需要在 target-duration 时间后定期重新加载它,并仅下载出现在列表中的新段(它们将在稍后删除)。

以下是一些相关的回答:IOS 设备可以使用 html5 视频和 phonegap/cordova 从本地文件系统流式传输 m3u8 分段视频吗?

您可以使用 AVAssetDownloadURLSession makeAssetDownloadTask 轻松下载 HLS 流。看看 Apples 示例代码中的 AssetPersistenceManagerhttps://developer.apple.com/library/content/samplecode/HLSCatalog/Introduction/Intro.html 使用 api.

的 Objective C 版本应该相当简单

是的,您可以下载通过 HLS 提供的视频流并稍后观看。

Apple 提供了一个非常简单的示例应用程序 (HLSCatalog)。代码相当简单。你可以在这里找到它 - https://developer.apple.com/services-account/download?path=/Developer_Tools/FairPlay_Streaming_Server_SDK_v3.1/FairPlay_Streaming_Server_SDK_v3.1.zip

您可以找到有关离线 HLS 流式处理的更多信息 here

我使用苹果代码 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()
    })