tvos AVPlayerViewController how to display subtitles tab on swipedown menu - objective-c

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!

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];

Related

ShareKit email form not always displaying

I'm using ShareKit to share objects to Twitter, Facebook, and Email. Twitter and Facebook works fine, but email doesn't. And I don't really know why.
It seems like it works quite randomly.
I set it up like this:
[self.customView.shareButton addTarget:self action:#selector(sharePost:) forControlEvents:UIControlEventTouchUpInside];
- (void)sharePost:(UIButton *)sender
{
NSURL *url = [NSURL URLWithString:self.currentPost.link];
// Add image.
NSString *imageUrlString = [NSString stringWithFormat:[link]];
NSURL *imageUrl = [NSURL URLWithString:imageUrlString];
UIImageView *postImage = [[UIImageView alloc] init];
[postImage setImageWithURL:imageUrl];
SHKItem *item = [SHKItem image:postImage.image title:[NSString stringWithFormat:#"%#", self.currentPost.title]];
[item setURL:url];
[item setMailBody:[NSString stringWithFormat:#"%# %#", self.currentPost.title, self.currentPost.link]];
// Get the ShareKit action sheet
// This works
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
// ShareKit detects top view controller (the one intended to present ShareKit UI) automatically,
// but sometimes it may not find one. To be safe, set it explicitly.
[SHK setRootViewController:self];
// Display the action sheet.
[actionSheet showInView:self.view];
}
Any ideas or what I'm missing or doing wrong?
Try to change the RootViewController and view you are using:
[SHK setRootViewController:self.view.window.rootViewController];
[actionSheet showInView:self.view.window.rootViewController.view];
Most likely your view controller hierarchy is somewhat messed up.

How to play audio from table

playbutton = (UIButton *)sender;
audiostring = [[NSMutableString alloc] init];
audiostring = [Audioarray objectAtIndex:playbutton.tag];
NSError *error;
audioPlayer = [[AVPlayer playerWithURL:[NSURL URLWithString:[audiostring valueForKey:#"url"]]] retain];
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: &error];
[audioPlayer play];
NSLog(#"audio array : %#",audioPlayer);
For playing next audio from cell. Please tell me correct answer.
You can use accessory view of the cell and attach a play button there. This Tutorial
explains how to create a custom accessory view which is a button actually,so it has a selector.Following this tutorial you can easily know on which cell the button has been pressed.So, user can press the play button of specific cell.Then take the name of the file associated with that cell and apply the things you learn from here to play your audio in background. Best of luck!

playing music by url

I want to play the music from internet by url.
I create a simply project that has one button with the following code:
NSURL *url = [[NSURL alloc] initWithString:#"http://someURL.mp3"];
NSError **err;
QTMovie *movie = [[QTMovie alloc] initWithURL:url error:err];
[movie play];
It works but with some delay (I think because it waits while file has been fully downloaded).
So what I need to do that the music starts to play immediately (when, for example, 10% of file has been downloaded)?
Use -[QTMovie autoplay] to automatically play the music once enough data has been downloaded.
In your case:
NSURL *url = [[NSURL alloc] initWithString:#"http://someURL.mp3"];
NSError **err;
QTMovie *movie = [[QTMovie alloc] initWithURL:url error:err];
[movie autoplay];
From the QTMovie class reference:
The autoplay method configures a QTMovie object to begin playing as soon as enough data is available that the playback can continue uninterrupted to the end of the movie.
If you can consider displaying a Quicktime window over your app, you can use this code :
NSString *url = #"http://someURL.mp3";
UIWebView* tempAudioPlayer = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[tempAudioPlayer loadHTMLString:[NSString stringWithFormat:#"<iframe frameborder=\"0\" width=\"0\" height=\"0\" src=\"%#\"></iframe>", url] baseURL:nil];
[self addSubview:tempAudioPlayer];
I first create a UIWebView which will not be displayed. Then I loadHTMLString a <iframe> in it, with the URL of my file as the src value. And I add it to my view. Quicktime appears immediately.

Change video url for MPMoviePlayerController instance rather than allocating new one

I have an MPMoviePlayerController named myMoviePlayer; I allocate and initialize it when my app loads:
NSString *moviePath = [bundle pathForResource:[movieName uppercaseString] ofType:#"mov" inDirectory:#"Videos"];
if(moviePath)
{
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
myMoviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[**myUI.view** setFrame:CGRectMake(80, 80, 600, 350)];
[self.view addSubview:myMoviePlayer.view];
myMoviePlayer.shouldAutoplay=NO;
}
There are two views in my app named imageView and videoView. I need to hide myMoviePlayer in imageView and display it again when my UI view is videoView.
Each time I show a movie, movieName will be different.
Right now, I am allocating and initializing myVideoPlayer each time my view changes to the movie view. Is it possible to set a new video url to myMoviePlayer without allocating it again?
Yes there is:
[myMoviePlayer setContentURL:[NSURL URLWithString:aMovieUrl]];
Just set the contentURL property of the MPMoviePlayerController instance.
Sharmain i got your problem...
you need to set the contentURL and then call Play method of mpmovieplayercontroller:
[myPlayer setContentURL:xyz];
[myPlayer play];
enjoy..!!
NSString *path = [[NSBundle mainBundle] pathForResource:#"myVideo" ofType:#"mp4"];
self.myPlayer = [[MPMoviePlayerController alloc] init];
self.myPlayer.view.frame = CGRectMake(0, 124, 768, 900);
self.myPlayer.shouldAutoplay = YES;
self.myPlayer.controlStyle = MPMovieControlStyleNone;
self.myPlayer.repeatMode = MPMovieRepeatModeOne;
self.myPlayer.fullscreen = YES;
self.myPlayer.movieSourceType = MPMovieSourceTypeFile;
self.myPlayer.scalingMode = MPMovieScalingModeAspectFit;
[self.view addSubview:myPlayer.view];
[myPlayer setContentURL:[NSURL fileURLWithPath:path]];
[myPlayer play];

Help on Objective C? My app keeps crashing?

I am creating an app for the Iphone/Itouch, but I keep on running into a couple of major leaks that just crash the app. In my game, right when I press play (from the home screen)It goes to another page that has the game on it. But, right after it appears, ind the console I get a Warning: Memory level=1. What could be happening? Here is my ViewDidLoad Method:
-(void)viewDidLoad {
[super viewDidLoad];
array = [[NSMutableArray alloc] initWithCapacity:100];
bulletArray = [[NSMutableArray alloc] initWithCapacity:100];
pos = CGPointMake(0.0,-5.0);
NSString *path = [[NSBundle mainBundle] pathForResource:#"GloriousMorning" ofType:#"mp3"];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
//float effects_Volume = [[NSUserDefaults standardUserDefaults] floatForKey:#"effectsVolume"];
//theAudio.volume = effects_Volume;
[theAudio play];
}
And also, a second question, since my game is a shooting game, the user presses a button titled "Fire". But, every time I test my app on a device, It crashes when I press the fire button. Here is my code for the fire button.
-(IBAction)Fire {
NSString *path = [[NSBundle mainBundle] pathForResource:#"gunShot" ofType:#"mp3"];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
//float effects_Volume = [[NSUserDefaults standardUserDefaults] floatForKey:#"effectsVolume"];
//theAudio.volume = effects_Volume;
[theAudio play];
//IBOutlet UIImageView *newBullet = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Bullet.png"]];
UIImageView *newBullet = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Bullet.png"]];
newBullet.frame = CGRectMake(0, 0, 10.0, 10.0);
newBullet.center = CGPointMake(239.0, 236.0);
[bulletArray addObject:newBullet];
[self.view addSubview:newBullet];
}
First, I create a sound. Then, I place a bullet right where the gun is currently located, and add it to an array so that every .01 of a second, in another bit of code, I can run through the array and check every bullet to detect collision.
Please tell me what I am doing wrong. Thanks!!!
The error when I click the Fire Button that makes the app crash is this:
GDB: Data Formatters temporarily unavailable, will retry after a 'continue'(unknown error loading shared library "
And Also I think I am making a huge leak when I try to play the audio, at least that's what someone told me. (If that is the case, please tell me how to fix it)
You alloc'd theAudio so you need to release it when you are done. Might be better to make that a ivar so you don't have to set up and tear down the audio every time they press the fire button.