6

iOS 10 以降、Apple はオフライン表示用に HLS (m3u8) ビデオのダウンロードをサポートしています。

私の質問は、HLS を再生中にのみダウンロードできるようにする必要があるかどうかです。または、ユーザーがダウンロード ボタンを押して進行状況を表示したときにダウンロードすることもできます。

Objective Cバージョンでこれを実装した人はいますか? 実際、私の以前のアプリは Objective C で作成されています。MP4 ではなく HLS のダウンロードのサポートを追加したいと考えています (以前は、オフライン ビュー用に MP4 をダウンロードしていました)。

私はこれに本当に必死です。実装されている場合は、考えやコードを共有してください。

4

4 に答える 4

4

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()
    })
于 2019-02-02T12:48:18.343 に答える
2

これを行う唯一の方法は、ダウンロードしたファイルをローカルで提供するように HTTP サーバーをセットアップすることです。

ライブ プレイリストはスライディング ウィンドウを使用します。target-duration 時間後に定期的にリロードし、リストに表示される新しいセグメントのみをダウンロードする必要があります (後で削除されます)。

関連する回答を次に示します。IOS デバイスは、html5 ビデオと phonegap/cordova を使用してローカル ファイル システムから m3u8 セグメント化されたビデオをストリーミングできますか?

于 2016-11-14T14:31:05.763 に答える
0

はい、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 ストリーミングの詳細については、こちらをご覧ください。

于 2017-07-28T12:16:58.967 に答える