AVPlayer not playing m3u8 from local file

I am trying to get AVPlayer to play a m3u8 playlist that is a local file.


I have narrowed this down to a simple test case using one of Apple's sample playlists:


https://tungsten.aaplimg.com/VOD/bipbop_adv_fmp4_example/master.m3u8


If i play this playlist from the remote url, AVPlayer plays this fine. However, if i download this playlist to a local file, and then hand AVPlayer the local file URL, AVPlayer will not play it. It just shows the crossed out play symbol.

Interestingly enough, this can be duplicated with Safari as well. Safari will play the remote playlist but not the local file. Also of note is that this behavior of AVPlayer is identical on iOS 10.2 as well as macOS 10.12.1. Also, playing an mp4 media file directly (not wrapped in a playlist) does not seem to have this issue, as it plays both from a remote URL as well as from a local file.


Inspecting the AVPlayerItem.error does not lead to anything useful either:


An unknown error occurred (-12865) The operation could not be completed 
Is anyone aware of any limitation that AVPlayer would not play a local playlist?


Thank you.

That's right you can't get m3u8 from the local filesystem. HTTP Live Streaming (HLS - the protocol built around m3u8s) does not let you get the content from the local filesystem. Basically, the naive idea of "I'll just download the files" isn't really workable in general.


However, starting with iOS 10 you can have the systen download HLS content for later playback. This done with AVAssetDownloadURLSession and related classes.

See the "Media Playback Programming Guide" - Working with HTTP Live Streaming

https://developer.apple.com/library/content/documentation/AudioVideo/Conceptual/MediaPlaybackGuide/Contents/Resources/en.lproj/HTTPLiveStreaming/HTTPLiveStreaming.html

Also, the sample code called "HLS Catalog"

and the session "What's New in HTTP Live Streaming" from WWDC 2016

Files with the .m3u8 file extension are the basis for the HTTP Live Streaming format used by Apple to stream video to iOS devices. These files contain MP3 playlist files and they are used by a variety of media applications and online radio stations. These files do not contain any actual audio data. M3U8 files are stored in a plain-text format. Files with the .m3u8 extension are a unicode version of the M3U file format.

Is Anyone got Solution For this?

I had the same problem. I was because I used the url received in param willDownloadTo of function:

urlSession(_:aggregateAssetDownloadTask:willDownloadTo:)

I always received the error -12865

To solve it when download finished in function : urlSession(_:task:didCompleteWithError:)

I create un bookmarkData with the previous mentioned url, and store it in User defaults with an unique identifier for de asset:

 let userDefaults = UserDefaults.standard
 let bookmark = try downloadURL.bookmarkData()
 userDefaults.set(bookmark, forKey: AssetIdentifier)

With this, when I need the url to playback a recover it from user defaults:

func localAssetFor(withID identifier: String) -> AVURLAsset? { let userDefaults = UserDefaults.standard guard let localFileLocation = userDefaults.value(forKey: identifier) as? Data else { return nil } var asset: AVURLAsset? var bookmarkDataIsStale = false do { let url = try URL(resolvingBookmarkData: localFileLocation, bookmarkDataIsStale: &bookmarkDataIsStale) if bookmarkDataIsStale { fatalError("Bookmark data is stale!") }

    let urlAsset = AVURLAsset(url: url)
    return urlAsset
} catch {
    fatalError("Failed to create URL from bookmark with error: \(error)")
}

}

With this changes I always have to playback offline content without error 12865. Hope this help Pedro

AVPlayer not playing m3u8 from local file
 
 
Q