【问题标题】:Downloading and playing offline HLS Content - iOS 10下载和播放离线 HLS 内容 - iOS 10
【发布时间】:2017-03-18 09:09:09
【问题描述】:

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

我的问题是:有必要只能在播放时下载HLS吗?或者我们可以在用户按下下载按钮并显示进度时下载。

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

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

【问题讨论】:

  • 如何在 iOS 10 中下载 HLS 以供离线查看?
  • 我下载的文件夹有 .ts 文件。
  • @NSPratik 你能告诉我如何播放离线 HLS 加密视频。我找到了stackoverflow.com/a/45957045/3887987,但可以排除问题。
  • @AmritTiwari 看到这个:stackoverflow.com/a/54493233/10758374
  • @AfnanAhmad 我也做了同样的事情,但下载单个视频文件正常,但我想下载 .ts 文件

标签: ios avplayer http-live-streaming m3u8 avassetdownloadtask


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以做到这一点的唯一方法是设置一个 HTTP 服务器,以便在您下载文件后在本地提供这些文件。

    实时播放列表使用滑动窗口。您需要在目标持续时间后定期重新加载它,并仅下载列表中出现的新片段(它们将在稍后被删除)。

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

    【讨论】:

      【解决方案3】:

      您可以使用 AVAssetDownloadURLSession makeAssetDownloadTask 轻松下载 HLS 流。查看 Apples 示例代码中的 AssetPersistenceManagerhttps://developer.apple.com/library/content/samplecode/HLSCatalog/Introduction/Intro.html 使用 Objective C 版本的 api 应该是相当简单的。

      【讨论】:

        【解决方案4】:

        是的,您可以下载通过 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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-09
          • 1970-01-01
          • 2022-11-10
          • 2020-03-13
          • 2020-03-10
          • 2020-12-08
          相关资源
          最近更新 更多