MPMoviePlayerController Wont Disappear - ios

I have been trying all day but for some reason on IOS6 im having so many issues with MPMoviePlayerController, it seemed people have had issues but not similar enough to mine:
The movie loads up and plays fine, when it completes the movie. I can loop the movie. The issue arises when I try to close and remove the movie like so:
If I add the controls to the Movie itself and hit the "Done" button, the movie sort of just pauses & never closes!
Any tips/ideas?
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayerController];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackLoaded:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:_moviePlayerController];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:kMovieOverlayViewTapped
object:nil];
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
-
Now on Playback loaded:
- (void)moviePlaybackLoaded:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:_moviePlayerController];
_moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
_moviePlayerController.controlStyle = MPMovieControlStyleDefault;
[_moviePlayerController prepareToPlay];
[_moviePlayerController.view setFrame:CGRectMake(38, 100, 250, 163)];
[_moviePlayerController play];
}
now to remove the video:
- (void)moviePlaybackComplete:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayerController];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerDidExitFullscreenNotification
object:_moviePlayerController];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:kMovieOverlayViewTapped
object:nil];
[_moviePlayerController stop];
[_moviePlayerController.view removeFromSuperview];
_moviePlayerController = nil;

Related

In objective-c how to play multiple video url one after another with out inbetween buffering

In my project I have to play multiple video one after another from server and I have done the following code. My video is playing well but the problem is that while one video is finished I am playing the next video in the -(void)aMoviePlaybackComplete method a black screen is appearing for 1 or 2 seconds. I dont want that and also want to play the video without buffering. Is there any better way to do this? If possible please kindly share valuable code or related links.
Thanks in advance.
-(void)viewDidAppear:(BOOL)animated
{
NSURL * myUrl = [NSURL URLWithString:[arrVideoLink objectAtIndex:0]];
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:myUrl];
moviePlayer.controlStyle=MPMovieControlStyleNone;
[vwVideoview addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, vwVideoview.frame.size.width, vwVideoview.frame.size.height);
moviePlayer.view.layer.zPosition = 1;
[moviePlayer prepareToPlay];
[moviePlayer play];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(aMoviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loadStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(nowPlayingMovie:)
name:MPMoviePlayerNowPlayingMovieDidChangeNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doneButtonClick:)
name:MPMoviePlayerWillExitFullscreenNotification
object:moviePlayer];
}
- (void)aMoviePlaybackComplete:(NSNotification*)notification
{
if (urlIndex+1<=arrVideoLink.count-1)
{
NSURL * myUrl = [NSURL URLWithString:[arrVideoLink objectAtIndex:urlIndex+1]];
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:myUrl];
moviePlayer.controlStyle = MPMovieControlStyleNone;
[vwVideoview addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, vwVideoview.frame.size.width, vwVideoview.frame.size.height);
moviePlayer.view.layer.zPosition = 1;
[moviePlayer prepareToPlay];
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(aMoviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loadStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
urlIndex++;
}
}else
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
[player.view removeFromSuperview];
}
}

MP movie controller removes when done button is clicked in iphone

I am playing video in MPmovieController it works fine but player removes when complete video is played.I want that when user pressed done button then video should stop here is the code i am using.
NSURL*myURL=[NSURL fileURLWithPath:url];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:myURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view setFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
Try this one:
NSURL *fileURL=[NSURL URLWithString:[[array objectAtIndex:videoid] valueForKey:#"VideoUrl"]];
self.mpPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[self presentMoviePlayerViewControllerAnimated:self.mpPlayer];
[self.mpPlayer.moviePlayer prepareToPlay];
self.mpPlayer.moviePlayer.shouldAutoplay=NO;
[self.mpPlayer.moviePlayer play];
- (void)moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer pause];
[self dismissMoviePlayerViewControllerAnimated];
// [moviePlayer.view removeFromSuperview];
}
may be it will help.
happy coding...
remove this line into your then it will work
[moviePlayerController.view removeFromSuperview];

iOS - Detect movie is finished

I am using MPMoviePlayerViewController to play a movie , I create a method which should detects when movie is finished then run a method :
- (void)movieFinishedWithSelector:(SEL)selectors {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(selectors)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[player moviePlayer]];
}
and use this method like this , but does not work .
[self movieFinishedWithSelector:#selector(finished)];
Am I missing something ?
The selectors parameter is already a selector. Don't use #selector:
- (void)movieFinishedWithSelector:(SEL)selector {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:selector
name:MPMoviePlayerPlaybackDidFinishNotification
object:[player moviePlayer]];
}
create a notication when you load the movie
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(myMovieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:movieController];
When finished the myMovieFinished will be called
-(void)myMovieFinished:(NSNotification *)aNotification
{
NSLog(#"%#",aNotification.userInfo);
int reason = [[[aNotification userInfo]valueForKey:MPMoviePlayerPlaybackDidFinishNotification]intValue];
if (reason==MPMovieFinishReasonPlaybackEnded) {
NSLog(#"Movie finished playing");
}
else if (reason==MPMovieFinishReasonUserExited)
{
NSLog(#"Movie finished because user exited");
}
else if (reason==MPMovieFinishReasonPlaybackError)
{
NSLog(#"movie finished playback error");
}
movieController=[aNotification object];
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:movieController ];
}
how did you define the selector? It should be:
- (void)movieDidFinish:(NSNotification*)notification

view doesn't come back after video plays

I'm running the following code. The video plays fine but after it finishes it just goes to a black srcreen, my original view never comes back. When I tap on the black screen i just see the message "loading....." Can someone please explain what I'm doing wrong. Thanks
- (IBAction)video:(UIBarButtonItem *)sender
{
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"IMG_0973" ofType:#"MOV"]];
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];
}
}
Add this notification Method
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification
object:player];
This method is called after your movie is loaded and in this method you add your moviePlayer view.
-(void)moviePreloadDidFinish:(NSNotification*)notification
{
moviePlayer.controlStyle=MPMovieControlStyleDefault;
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
[moviePlayer setFullscreen:YES animated:YES];
}
In your video IBAction, you need to add the subview before you tell it to play. Switch the lines [moviePlayer play] and [self.view addSubview:moviePlayer.view]. Let us know it it works! Actually you may need to place moviePlayer play even after the full screen line.
i think this will help you .....
-(void)playVideo
{
NSString *contentURL = [[NSBundle mainBundle] pathForResource:#"xyz" ofType:#"mp4"];
MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:contentURL]];
if (moviePlayerViewController)
{
[moviePlayerViewController.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
[moviePlayerViewController.moviePlayer setFullscreen:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MovieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
[moviePlayerViewController.moviePlayer play];
[navi presentModalViewController:moviePlayerViewController animated:NO];
[moviePlayerViewController release];
moviePlayerViewController = nil;
}
}
-(void)MovieFinished:(NSNotification *)notification
{
MPMoviePlayerController *player = (MPMoviePlayerController *)notification.object;
[player stop];
[[NSNotificationCenter defaultCenter] removeObserver:self];
//do rest of the stuff
}

NSNotificationCenter removing wrong observers?

Is there any corner case behaviors for removeObserver:name:object:? In the following block of code, my observer isn't being registered properly:
- (void)setPlayerItem:(AVPlayerItem *)playerItem {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:nil
object:playerItem];
[playerItem addObserver:self
forKeyPath:kStatus
options:0
context:(__bridge void*)self];
[playerItem addObserver:self
forKeyPath:kPlaybackBufferEmpty
options:0
context:(__bridge void*)self]; // For adding a buffering activity indicator
id temp = playerItem_;
playerItem_ = [playerItem retain];
[[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:temp];
[temp removeObserver:self forKeyPath:kPlaybackBufferEmpty];
[temp removeObserver:self forKeyPath:kStatus];
[temp release];
}
However, if I change the order around to:
- (void)setPlayerItem:(AVPlayerItem *)playerItem {
[playerItem addObserver:self
forKeyPath:kStatus
options:0
context:(__bridge void*)self];
[playerItem addObserver:self
forKeyPath:kPlaybackBufferEmpty
options:0
context:(__bridge void*)self]; // For adding a buffering activity indicator
id temp = playerItem_;
playerItem_ = [playerItem retain];
[[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:temp];
[temp removeObserver:self forKeyPath:kPlaybackBufferEmpty];
[temp removeObserver:self forKeyPath:kStatus];
[temp release];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:nil
object:playerItem];
}
All the notifications post just fine. This leads me to believe something strange is happening when I call:
[[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:temp];
Am I missing something really obvious here? I'm on iOS 6 with no ARC.
Found the answer. Turns out it has to do with passing in nil for the observer name.
Calling [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:temp];
will remove self from observing any notifications posted by temp.
However, in the corner case that temp is nil, this line of code will remove self as an observer all together.
Name shouldn't be nil. Did you try giving your observer a name?
#Lee is correct that the name should not be nil but it also should not be the name of the observer. Rather it should be the name of the notification that you are registering to observe. e.g., UIDeviceOrientationDidChangeNotification.
Add the name of the notification that you want to observe in that param and also pass it as the name param when you remove observer

Resources