Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tvos AVPlayerViewController how to display subtitles tab on swipedown menu

In tvOS, in the menu that is being displayed when a user swipes down on the remote, that shows "subtitles, audio & info" on other movie apps, how to create another tab with buttons?

Below is my code:

AVMutableMetadataItem *titleMetadataItem = [[AVMutableMetadataItem alloc] init];
titleMetadataItem.locale = NSLocale.currentLocale;
titleMetadataItem.key = AVMetadataCommonKeyTitle;
titleMetadataItem.keySpace = AVMetadataKeySpaceCommon;
titleMetadataItem.value = @"The Title";

NSArray *externalMetadata = [[NSArray alloc] initWithObjects:titleMetadataItem, nil];

_player.player.currentItem.externalMetadata = externalMetadata;

Can someone please tell me how can I create a buttons in the swipe down menu of an AVPlayerViewController so that a user can toggle between turning off or turning on the subtitles? I do not have the srt files embedded in the video. Instead I have a separate subtitle parser and I display it on a label. I was able to get info section to show with text but is there any way to add buttons?

OR how I can add a subtitle option to the video? This does not work: _player.requiresFullSubtitles = YES;

Thanks!

like image 663
Jenel Ejercito Myers Avatar asked Nov 12 '15 19:11

Jenel Ejercito Myers


1 Answers

The AVPlayerViewController only loads subtitles if they're embedded in the HLS streams, and that is also the only legal way to show the "Subtitles" tab on the swipe down menu.

I built a utility for dynamically adding VTT subtitles to HLS (m3u8) streams called ACHLSCaptioningServer (https://github.com/acotilla91/ACHLSCaptioningServer).

Note: If you only have access to SRT files you'll need to find a way to convert SRTs to VTTs.

How to use:

NSURL *origStreamURL = your_original_stream-url;
NSURL *vttFileURL = your_vtt_file_url;

NSURL *streamURL = [[ACHLSCaptioningServer sharedInstance] getCaptionedHLSStreamFromStream:origStreamURL vttURL:vttFileURL];

//
// play stream
//

AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:streamURL options:nil];

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:videoAsset];

AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];

AVPlayerViewController *avPlayerController = [[AVPlayerViewController alloc] initWithNibName:nil bundle:nil];
[avPlayerController setPlayer:player];
[avPlayerController.view setFrame:self.view.frame];
[self addChildViewController:avPlayerController];
[self.view addSubview:avPlayerController.view];
[avPlayerController didMoveToParentViewController:self];
like image 104
Alejandro Cotilla Avatar answered Nov 12 '22 15:11

Alejandro Cotilla