Download and play offline HLS content - iOS 10

Starting with iOS 10, Apple has provided support for downloading HLS video (m3u8) for offline viewing.

My question is: do we only need to download HLS when playing it? Or we can just download when the user clicks the download button and shows the progress.

Has anyone implemented this in the Objective-C version? In fact, my previous application was created in Objective C. Now I want to add support for downloading HLS, not MP4 (I previously downloaded MP4 for offline viewing).

I am really desperate. Please share your thoughts or any code, if implemented.

+4
source share
4 answers

- HTTP- .

Live . , ( ).

: IOS m3u8 html5 phonegap/cordova?

+1

HLS AVAssetDownloadURLSession makeAssetDownloadTask. AssetPersistenceManager : https://developer.apple.com/library/content/samplecode/HLSCatalog/Introduction/Intro.html Objective C api.

+1

apple code 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

Now you can use the saved HSL content to play 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 home directory
    let assetUrl = baseUrl.appendingPathComponent(savedLink!) //append the saved link to home path

Now use the path to play video in AVPlayer

let avAssest = AVAsset(url: url)
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()
    })
0
source

Source: https://habr.com/ru/post/1659828/


All Articles