I am using m3u8 video format for streaming the video and now I need to display subtitles for the same.
I searched in Apple Documentation and found that I can achieve this by using the closedCaptionDisplayEnabled
property of AVPlayer
.
I am interested to know what should be the format of subtitles? Will the .srt format do?
Also can I achieve the same using MPMoviePlayerController
?
Any help is appreciated.
Update 10/30/2018: It's worth checking this answer by an Apple engineer (Thanks to @allenlini for pointing it out). He suggests a solution involving
AVAssetResourceLoaderDelegate
. I haven't tried it myself, but it might be a better solution than mine below.Original Answer:
It seems as if referencing your WebVTT files in your m3u8 streaming description is the officially supported way. Adding them "after the fact" seems to not be officially supported (See this statement by an Apple engineer (bottom of the page)).
That - however - does not mean that you can't get it to work ;-). With the help of this great presentation and sample project (ZIP) by Chris Adamson, this post on the Apple Developer Forums and this Ray Wenderlich tutorial by Abdul Azeem, I was able to get it to work. This is a modified version of Abdul Azeem's sample code and Chris' sample project.
Note how you need to use
AVMediaTypeText
instead ofAVMediaTypeSubtitle
. This seems to be a bug in iOS.Both AVPlayer and MPMoviePlayerController can display subtitles.
The difference seems to be that with AVPlayer, you control whether or not the subtitles are displayed using the closedCaptionDisplayEnabled property.
With MPMoviePlayerController, the user controls whether or not subtitles are displayed using a switch in the Setting app. You get no programatic control of this in the app.
The WWDC 2013 iOS app uses WebVTT files for its subtitles (credit to Nicholas Riley for discovering this). For whatever reason, there are about 50 (exact number varies) for each session.
I have no idea whether WebVTT is supported at the AVFoundation or MPMoviePlayer level, or whether the app downloads and parses the subtitles files itself.
A quick search for “webvtt m3u8” turned up a reference to this HTTP Live Streaming Draft, which suggests that you may be able to make this work by simply referencing the WebVTT files in your m3u8 playlist. I don't have an example for you, though, since I wasn't able to trivially guess the m3u8 URL for a WWDC session.
This is the Swift version of @JohannesFahrenkrug answer. Hope this is useful for someone: