Downloading and playing offline HLS Content - iOS 10
Asked Answered
K

4

7

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.

Kermanshah answered 4/11, 2016 at 13:44 Comment(7)
How do you download HLS in iOS 10 for offline viewing?Disarmament
I download folder having .ts file.Kermanshah
@Kermanshah can you please share me an idea how to play offline HLS encrypted video. I found https://mcmap.net/q/671463/-playing-offline-hls-with-aes-128-encryption-ios but can shut out the problem.Biophysics
@AmritTiwari see this: https://mcmap.net/q/1499657/-downloading-and-playing-offline-hls-content-ios-10Nicholnichola
@Nicholnichola I have done same as that but downloading is working for single video file fine but I want to download the .ts filesBiophysics
@AmritTiwari I have not worked with .ts files. Could you provide a link to such files so I can test and see what's not working ?Nicholnichola
@Nicholnichola can you please send me your contact. So I can directly connect with you. Because I have to keep url secret.Biophysics
V
2

The only way you can do this is to set up an HTTP server to serve the files locally after you've downloaded them.

The Live playlist uses a sliding-window. You need to periodically reload it after target-duration time and download only the new segments as they appear in the list (they will be removed at a later time).

Here are some related answers: Can IOS devices stream m3u8 segmented video from the local file system using html5 video and phonegap/cordova?

Vaas answered 14/11, 2016 at 14:31 Comment(0)
N
5

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()
    })
Nicholnichola answered 2/2, 2019 at 12:48 Comment(0)
V
2

The only way you can do this is to set up an HTTP server to serve the files locally after you've downloaded them.

The Live playlist uses a sliding-window. You need to periodically reload it after target-duration time and download only the new segments as they appear in the list (they will be removed at a later time).

Here are some related answers: Can IOS devices stream m3u8 segmented video from the local file system using html5 video and phonegap/cordova?

Vaas answered 14/11, 2016 at 14:31 Comment(0)
C
1

You can easily download an HLS stream with AVAssetDownloadURLSession makeAssetDownloadTask. Have a look at the AssetPersistenceManager in Apples Sample code: https://developer.apple.com/library/content/samplecode/HLSCatalog/Introduction/Intro.html It should be fairly straight forward to use the Objective C version of the api.

Chloric answered 12/6, 2017 at 12:24 Comment(0)
L
0

Yes, you can download video stream served over HLS and watch it later.

There is a very straight forward sample app (HLSCatalog) from apple on this. The code is fairly simple. you can find it here - https://developer.apple.com/services-account/download?path=/Developer_Tools/FairPlay_Streaming_Server_SDK_v3.1/FairPlay_Streaming_Server_SDK_v3.1.zip

You can find more about offline HLS streaming here.

Latoria answered 28/7, 2017 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.