Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

avplayer - Downloading and playing offline HLS Content - iOS 10

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

My question is: Is it necessary that we can only download HLS when it is being played ? Or we can just download when user press download button and show progress.

Does anyone has implemented this in Objective C version? Actually my previous App is made in Objective C. Now I want to add support for downloading HLS rather than MP4 (previously I was downloading MP4 for offline view).

I am really desperate to this. Please share thoughts or any code if implemented.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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")
}

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

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()
    })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...