Quitting MPMediaPlayerController view - ios

I am writing a simple method to start a streaming video full-screen and letting the user quit pressing the "Done" button. Problem is, I can't remove the MPMediaPlayerController view, or perhaps I am doing it the wrong way.
- (IBAction)playVideoButtonPressed:(id)sender {
NSURL *url = [NSURL URLWithString:#"http://mysite.com/video.mov"];
mp = [[MPMoviePlayerController alloc] initWithContentURL: url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerPlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
if ([mp respondsToSelector:#selector(setFullscreen:animated:)]) {
mp.controlStyle = MPMovieControlStyleFullscreen;
mp.shouldAutoplay = YES;
[self.view addSubview:mp.view];
[mp.view setTag:3];
[mp setFullscreen:YES animated:YES];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mp];
[mp play];
}
- (void)moviePlayBackDidFinish:(id)sender {
NSLog(#"Quitting MoviePlayer");
[mp.view removeFromSuperview];
}
The idea is that the MPMediaPlayerController view is called clicking on a button in the app, and it is dismissed by clicking the "Done" video or when the video ends.

I should have used MPMoviePlayerViewController.

Related

MPMoviePlayerController seek bar is not appearing after applying natural size

I am using MPMoviePlayerController for playing video files and I am able to apply the natural size of the video with the help of MPMovieNaturalSizeAvailableNotification. But after adding this notification seek bar is not appearing in the controlStyle (all other buttons are present). If I make the video frame hard coded (i.e. without considering the natural height) I will not get this issue.
Here is my code
// Declartion class level
MPMoviePlayerController *moviePlayer;
float naturalHeight;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
moviewPlayer = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieReadyForDisplay:)
name:MPMoviePlayerReadyForDisplayDidChangeNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieNaturalSizeAvailable:)
name:MPMovieNaturalSizeAvailableNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[self.view addSubview:moviePlayer.view];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayer.fullscreen = NO;
moviePlayer.scalingMode = MPMovieScalingModeFill;
moviePlayer.shouldAutoplay = NO;
//I am able to get the seek bar here if I set the static height . But I want natural height here.
// moviePlayer.frame = CGRectMake(0, 0, self.view.frame.size.width , naturalHeight);
}
-(void) movieNaturalSizeAvailable:(NSNotification *)notification{
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMovieNaturalSizeAvailableNotification object:player];
naturalHeight = player.naturalSize.height;
if(naturalHeight > self.view.frame.size.height) {
naturalHeight = self.view.frame.size.height;
}
}
-(void) movieReadyForDisplay:(NSNotification *)notification{
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerReadyForDisplayDidChangeNotification object:player];
NSLog(#">> %f ", naturalHeight); //OK here
[player view].frame = CGRectMake(0, 0, self.view.frame.size.width , naturalHeight);
player.view.center = self.view.center;
[player prepareToPlay];
[player play];
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player stop];
[player.view removeFromSuperview];
}
#end
I believe this issue might be because the natural size calculations of
video player occur after it sets its components frames. If I don't use natural size, it works perfectly. How can I fix this issue?
try this
before starting the video
player.controlStyle = MPMovieControlStyleNone;
and when movie starts playing then set
player.controlStyle = MPMovieControlStyleFullscreen;
You can get play callback from MPMediaPlayback protocol.

How to play mp4 video from URL over HTTPS on iOS

I'm trying to play a video in mp4 format from URL over HTTPS using MPMoviePlayerController, but video is not playing and I receive an error in logs:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
Is any way to play this kind of video on iOS?
Here's my code:
#import "FirstViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#interface FirstViewController ()
#property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
#end
#implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self playBtnPressed];
}
-(void)playBtnPressed
{
NSURL *url=[[NSURL alloc] initWithString:#"https://....mp4"];
_moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDonePressed:) name:MPMoviePlayerDidExitFullscreenNotification object:_moviePlayer];
_moviePlayer.controlStyle=MPMovieControlStyleDefault;
//moviePlayer.shouldAutoplay=NO;
[_moviePlayer play];
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}
- (void) moviePlayBackDonePressed:(NSNotification*)notification
{
[_moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:_moviePlayer];
if ([_moviePlayer respondsToSelector:#selector(setFullscreen:animated:)])
{
[_moviePlayer.view removeFromSuperview];
}
_moviePlayer=nil;
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[_moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
if ([_moviePlayer respondsToSelector:#selector(setFullscreen:animated:)])
{
[_moviePlayer.view removeFromSuperview];
}
}
#end
Probably video aspect ratio issue try setting aspect ratio
[player setScalingMode:MPMovieScalingModeAspectFit];
player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
Or check if the video file is there in the specified URL location
It seems to be an iOS 7 issue.
Apparently, moviePlayer.movieSourceType = MPMovieSourceTypeStreaming; does not work anymore.
For me, replacing it with MPMovieSourceTypeFile solved this error.
Use following code it will work for you. Be sure that you have the notification observer declared before playing movie, as follows:
[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController.moviePlayer];
moviePlayerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[moviePlayerController.moviePlayer prepareToPlay];
[self presentMoviePlayerViewControllerAnimated:moviePlayerController];
[moviePlayerController.moviePlayer play];

MPMoviePlayerController MPMovieControlStyleFullscreen strangeness

I have the following code to play a video
moviePlayer1 =[[MPMoviePlayerController alloc] initWithContentURL:firstMovieURL1];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doneButtonClicked) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[moviePlayer1 view] setFrame: [self.view bounds]]; // frame must match parent view
[self.view addSubview: [moviePlayer1 view]];
[moviePlayer1 play];
-(void)doneButtonClicked
{
NSLog(#"doneButtonClicked ...");
[moviePlayer1 stop];
[moviePlayer1.view removeFromSuperview];
}
If I use the above code my screen looks like this. I have to click on the little arrow icon and then the screen again to show controls (forward/backwards/done button)
so what I did was add this controlStyle to my code. I see all the buttons now as soon as the player is launched but and now my done button doesn't close the window instead it just pauses the movie. What happened?
moviePlayer1.controlStyle=MPMovieControlStyleFullscreen;
[moviePlayer1 play];
This is how you do it. God forbid if apple makes this logic any easy on us.
[[moviePlayer1 view] setFrame: [self.view bounds]]; // frame must match parent view
[self.view addSubview: [moviePlayer1 view]];
moviePlayer1.controlStyle=MPMovieControlStyleFullscreen;
[moviePlayer1 prepareToPlay];
[moviePlayer1 play];
//use this instead
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doneButtonClicked:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
-(void)doneButtonClicked:(NSNotification*)notification
{
NSLog(#"doneButtonClicked ...");
NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if ([reason intValue] == MPMovieFinishReasonUserExited)
{
// done button clicked!
[moviePlayer1 stop];
[moviePlayer1.view removeFromSuperview];
}
}

How to make status bar not to disappear in MPMoviePlayerController in iOS?

In MPMoviePlayerController, when ever the controls disappear, even the status bar is disappearing with it. Since i want the status bar to appear even when the control disappears, i placed the below code
[[UIApplication sharedApplication] setStatusBarHidden:NO];
But the above code is not making any difference, teh status bar is getting disappeared along with the player controls. How to solve this problem.
Please find the code below, and let me know how to rectify it.
- (void) readyPlayer {
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if ([mp respondsToSelector:#selector(loadState)])
{
// Set movie player layout
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setFullscreen:YES];
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
} else {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePreloadDidFinish:) name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:nil];
}
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
NSLog(#"moviePlayerLoadStateChanged");
// Unless state is unknown, start playback
if ([mp loadState] != MPMovieLoadStateUnknown)
{
// Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
// Rotate the view for landscape playback
[[self view] setBounds:CGRectMake(0, 0, 768, 1000)];
// Set frame of movieplayer
[[mp view] setFrame:CGRectMake(0, 0, 768, 1000)];
// Add movie player as subview
[[self view] addSubview:[mp view]];
// Play the movie
[mp play];
}
}
- (void) moviePreloadDidFinish:(NSNotification*)notification {
// Remove observer
NSLog(#"moviePreloadDidFinish");
[[NSNotificationCenter defaultCenter] removeObserver:nil
name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:nil];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
// Play the movie
[mp play];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
NSLog(#"moviePlayBackDidFinish");
[[UIApplication sharedApplication] setStatusBarHidden:NO];
// Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissModalViewControllerAnimated:YES];
}
This is a well-known issue of MPMovieControlStyleFullscreen. Simply use the MPMovieControlStyleEmbedded controlStyle and you are good to go.
And, by the way, that is the more adequate controlStyle for embedded usage anyways.

Mpmovieplayercontroller in iPad - No Video only audio

I am developing an iPad app where I am playing the videos stored within the app itself. I am representing the list of videos in a table view. When I select a row, the video
corresponding to that row plays. But while performing this, sometimes screen goes black , no video is visible but only audio is playing.
I am aware that making the video play in fullscreen mode or using the MPMoviePlayerViewController eliminates this problem. But my requirement is that I don't want to play the movie in fullscreen initially. Please guide me on how this can be achieved.
-(void)playMovie {
MPMoviePlayerController *movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieUrl];
self.moviePalyer = movieController;
[movieController release];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePalyerDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object: self.moviePalyer];
self.moviePalyer.view.frame = CGRectMake(240, 0, 561, 313);
self.moviePalyer.view.backgroundColor = [UIColor clearColor];
[self.moviePalyer prepareToPlay];
[self.view addSubview: self.moviePalyer.view];
[self.moviePalyer play];
}
-(void)moviePalyerDidFinish:(NSNotification*)notification
{
[moviePalyer.view removeFromSuperview];
[moviePalyer stop];
moviePalyer.initialPlaybackTime = -1.0;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePalyer];
[moviePalyer release];
moviePalyer = nil;
}
NOTE: This is on ipad simulator
I have solved the issue. Previously I was creating the MPMoviePlayerController for each of the row selection, and releasing it in the moviedidfinish notification method .But now, instead of creating movieplayercontroller for every row selection, I am reusing the MPMoviePlayerControler object which has been already created.
Following is the Code snippet:
-(void)playMovie
{
if(self.moviePalyer == nil)
{
MPMoviePlayerController *movieController = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.moviePalyer = movieController;
[movieController release];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePalyerDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePalyer];
self.moviePalyer.repeatMode = MPMovieRepeatModeOne;
self.moviePalyer.view.frame = CGRectMake(240, 0, 561, 313);
self.moviePalyer.view.backgroundColor = [UIColor clearColor];
}
else
{
[self.moviePalyer setContentURL:url];
[self stopMovie];
}
[self.view addSubview: self.moviePalyer.view];
[self.moviePalyer play];
}
-(void)stopMovie
{
[self.moviePalyer.view removeFromSuperview];
[self.moviePalyer stop];
self.moviePalyer.initialPlaybackTime = -1.0;
}
I am releasing the moviePlayer object in the dealloc method.
Hope this helps who is having the same issue.

Resources