how to add subtitles in AVPlayer

I need to add subtitles from a url file for my video.


player = AVPlayer(URL: NSURL(string: "http:////playlist.m3u8")!)
        player?.play()
        player?.closedCaptionDisplayEnabled=true

Can someone provide me proper solution for this.

Accepted Reply

Hi Manish,


You need to include the URL to your webVTT subtitles inside of your master playlist.


Please visit the streaming section of our Developer website: https://developer.apple.com/streaming/


On that page, you will find links to resources on implementing HTTP Live Streaming, including a link to the WWDC session entitled Subtitles in HTTP Live Streaming using WebVTT

Replies

Hi Manish,


You need to include the URL to your webVTT subtitles inside of your master playlist.


Please visit the streaming section of our Developer website: https://developer.apple.com/streaming/


On that page, you will find links to resources on implementing HTTP Live Streaming, including a link to the WWDC session entitled Subtitles in HTTP Live Streaming using WebVTT

I included WebVTT subtitles in my master playlist.But i still have a doubt that how can i manage multilanguage subtitles.In Apple TV i went to settings ->Audio and video then subtitles.I choose Off option inside the subtitles tag but still the subtitles are showing.Can someone Help me with this issue

Are you still calling "player?.closedCaptionDisplayEnabled=true"? There's no need to do that.


If you author your HLS playlist properly with the appropriate language tags, the user can enable captions in the language of their choice, or disable them completely as well.

No sir i was trying to use the global subtitle option available in Audio & video settings.When i tried to turn it OFF it is still showing.IS this a issue of simulator?

No, it's likely a matter of how the HLS playlist is authored. Are you able to paste the master playlist here as a code snippet?

How do you include the WebVTT subtitles in master playlist?

Hi Jenel,


In my initial response I included a link to the WWDC session video entitled Subtitles in HTTP Live Streaming using WebVTT, which walks through this in detail.


Our advanced stream sample also includes subtitles using WebVTT: https://developer.apple.com/streaming/examples/


Thanks.

but for this, it seems that you have to have the subtitle files embedded within the video is that correct?

Nope - you have them as separate, chunked WebVTT files. They are separate, elementary streams in the master playlist, and are not muxed into the video transport stream.


We do support CEA-608/708 closed captions as well, and those would be embedded into the video transport stream.

Are CEA-608 closed captions supported on Apple TV? I play back a m3u8 without issue that uses WebVTT but AVPlayerViewController fails to present the subtitles menu for video with embedded CEA-608.

We are having the same issue. WebVTT captions are fine. CEA 608 captions in our live linear stream are not being recognized by AVPlayer

TidBits wrote:

>Are you still calling "player?.closedCaptionDisplayEnabled=true"? There's no need to do that.

>

>If you author your HLS playlist properly with the appropriate language tags, the user can enable captions in the language of their choice, or disable them >completely as well.


I have asked a question regarding what you wrote here: http://stackoverflow.com/questions/37415311/ios-choosing-specific-closed-captions-eia-608


Any chance that you can add some details about it?

thank you!

-Itay

I modifed the master playlist using the resource loader to add EXT-X-MEDIA tags for the subtitles and url to the playlist for each subtitle. The resource loader responded to the subtitle playlist requested with a single (not segmented) webvtt file. The subtitle selection showed up in the native AVPlayer. But selecting it does nothing. Is there anyway to investigate what's wrong? Do I need to segment the webvtt for it to work?

I got this working. It was a horrible experience but I did succeed. My full explanation is too long to post here so I've stuck my long explanation and sample code on Github: https://github.com/kanderson-wellbeats/sideloadWebVttToAVPlayer and on StackOverflow: https://stackoverflow.com/a/67308098/15788389 .