MPMoviePlayerController cannot be removed using removeFromSuperView - objective-c

I need to remove a MPMoviePlayerController from a View.
I tried this.
[moviePlayerController stop];
[moviePlayerController.view removeFromSuperview];
the video stops, but the view is not removed. I guess [moviePlayerController.view removeFromSuperview]; does not work. What could be the reason ? Any Solution to this prolem ..?
Thanks.

This problem generally occurs because of the player get deallocated.The solution is that declare the player instance in .h with property "strong".
#property (nonatomic,strong) MPMoviePlayerController* mpController;

Its a known problem that id you are using ARC, then you HAVE TO add the player to your .h because it does still get released if you declare it locally.
#property (nonatomic, strong) MPMoviePlayerController* controller;
To add the view:
self.controller = [[MPMoviePlayerController alloc] initWithContentURL:YOURVIDEOURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.controller];
self.controller.controlStyle = MPMovieControlStyleDefault;
self.controller.shouldAutoplay = YES;
[self.view addSubview:self.controller.view];
[self.controller setFullscreen:YES animated:YES];
And then to remove the view:
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];
}
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}

Not entirely sure, but since I had the problem of the movieplayer not showing up because of auto deallocating, I guess you could just set moviePlayerController = nil;.
Not Totally sure if the view will disappear, but worth a try!

Try dismissViewController:animated: That will probably work.

[moviePlayerController stop];
[moviePlayerController setContentURL:nil];
[moviePlayerController.view removeFromSuperview];
this is running well in my project

For me, I tried all of these:
[moviePlayer stop];
[moviePlayer setContentURL:nil];
[moviePlayer.view removeFromSuperview];
moviePlayer = nil;
And nothing worked. I figured out it had to due with my MPMoviePlayerController entering full screen. The fix?
[moviePlayer setFullscreen:NO animated:YES];

Related

Loading the same ViewController after watching MPMoviePlayerController

I have a ViewController which displays the information about the selected show, and the play button of the MPMoviePlayerController is there. Now, the issue is when I clicked on the full screen of the player then clicked done. It is supposed to go back to the ViewController which displays the info, but it isn't. But it is going in to the viewDidLoad of the ViewController and it doesn't display anything at all. I feel like something is on the top of the ViewController or something like that
Here's how I remove the player controller
- (void)dealloc
{
self.delegate = nil;
self.activityIndicator = nil;
[self deregisterMoviePlayerNotifications];
[self.player.view removeFromSuperview];
[self removeFromSuperview];
[self stopVideo];
self.player = nil;
}
You should use AVPlayerViewController as MPMoviePlayerController is deprecated. You can use this code on play button
-(void)playButton{
AVPlayer *player = [[AVPlayer alloc]initWithURL:urlVideo];
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
controller.videoGravity = AVLayerVideoGravityResizeAspectFill;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:player.currentItem];
controller.videoGravity = AVLayerVideoGravityResizeAspect;
[self presentViewController:controller animated:YES completion:nil];
[player play];
}
-(void)playerItemDidReachEnd:(NSNotification*)notification{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self dismissViewControllerAnimated:YES completion:nil];
}
Note : Don't forget to add AVFoundation.framework.

MPMoviePlayer does not disappear on [moviePlayer.view removeFromSuperview] and [moviePlayer release]

I have a problem with MPMoviePlayerController.When I am watching a video, and hit the 'Done' button on the left top, the MoviePlayer does not disappear, even though the code seems to be called:
NSURL *url = [NSURL URLWithString:article.enclosureLink];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Set movie player layout
[moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[moviePlayer setFullscreen:YES];
// May help to reduce latency
[moviePlayer prepareToPlay];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieReadyToPlay:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:moviePlayer];
And the selectors:
- (void) movieReadyToPlay:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
if(moviePlayer.loadState == MPMovieLoadStatePlayable){
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
//moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer play];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer setFullscreen:NO animated:YES];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
NSLog(#"Finished movie!");
}
This looks to me a very straight forward code, but I must make a stupid mistake. The NSLog shows that the function is called, but the player stays where it is and there is no way of getting rid of it.
Also, the very fact that the player is still operational after the alleged release seems to indicate that there is something fundamental wrong, I just don't see what.
Is there anybody who has a suggestion?
[Update:]
Strangely in the iPhone Simulator it works fine!
[Update2:]
I tried and created a specific UIviewcontroller, even though it is not the way I want to do it as the animations are not nice. But what I learned is that I have the same problem. It seems to hav to do something with dismissing the player, but it starting again.
When I put [self.moviePlayer setFullscreen:YES animated:YES]; in viewDidApear, and click the 'Done' button in the player, the player, the video starts over again when I hit the Done button (the viewDidAppear is called again). So something is triggered, so it seems to me, to make the video start again.
If I put it viewDidLoad, then the system works, but the graphics are mixed and confused...
Any help is really, really appreciated as I spend two days on this now without making head or tail of it!
For me, I tried all of these:
[moviePlayer stop];
[moviePlayer setContentURL:nil];
[moviePlayer.view removeFromSuperview];
moviePlayer = nil;
And nothing worked. I figured out it had to due with my MPMoviePlayerController entering full screen. The fix?
[moviePlayer setFullscreen:NO animated:YES];
Adding
[moviePlayer stop]
before
[moviePlayer.view removeFromSuperview]
may work.
Update:
If this doesn't work then try setting controlstyle to MPMovieControlStyleNone before removing the subview.Most of the time the controlStyle causes such problems.

Problem with video on NavigationController + TabBarController

I have an application with a TabBarController containig two TabBars with a NavigationController.
In the first tabbar, I have a ViewController containing some buttons. One of these buttons enable the user to play video. And all run fine for the moment.
My problem is that when the video is being played and I move to the second TabBar, the video continue playing (I hear the sound), then if I come back to the first TabBar (where there is my buttons) the application crashes without any notification.
I don't understand if it's a memory problem or something else.
Any suggestions?
Here is the function used :
-(IBAction)playMovie:(id)sender
{
videoView = [[UIViewController alloc]initWithNibName:#"video" bundle:nil];
videoView.title = #"title";
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"video.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
testbtnAppDelegate *appDelegate = (testbtnAppDelegate *)[[UIApplication sharedApplication] delegate];
videoView.view=moviePlayerController.view;
[moviePlayerController play];
[appDelegate.navController pushViewController:videoView animated:YES];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
[moviePlayerController pause];
before u go to secondtab pause.

iPad videoPlayerDidFinishPlaying callback method not responding

I am facing a problem in ipad video incorporating. My code works fine I mean it plays the video, but once the video reaches to its end. The callback method is not called.
This method is called when play video button is pressed.
-(IBAction) playVideo : (id) sender
{
[self initPlayingVideo:#"toyVid.mp4"];
}
This method handles the playing of video.
-(void) initPlayingVideo: (NSString *) videoFile
{
NSString *moviePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:videoFile];
theMovie = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
theMovie.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
theMovie.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self.view addSubview:theMovie.view];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(videoPlayerDidFinishPlaying
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
videoPlayer = [theMovie moviePlayer];
[videoPlayer play];
}
This is the callback method.
-(void) videoPlayerDidFinishPlaying: (NSNotification*)aNotification
{
theMovie = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie.moviePlayer];
[videoPlayer stop];
[theMovie.moviePlayer release];
[videoPlayer release];
[theMovie.view removeFromSuperview];
}
Where I am doing mistake? Please guide.
Regards
Ranjan
Did you miss the : and ) in your selector? I guess ) may be your typo otherwise you cannot compile your codes. Your selection takes one parameter. It should be:
selector:#selector(videoPlayerDidFinishPlaying:)
That will match to your instance method. I guess you don't have one without parameter.

MPMoviePlayerController problems on iPad

I'm trying to use the MPMoviePlayerController class on the iPad.
Here's my code:
multimediaPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
multimediaPlayer.movieControlMode = MPMovieControlModeDefault;
[multimediaPlayer play];
and this works very well on the iPhone but it don't want to run on the iPad. I hear the sound of the video, but the movie doesn't playing.
Why it can be this problem?
Below code working perfect for my application. Hope it would do same for you.
The main thing is to set the frame of mpMoviePlayerController's frame. if you don't do it, it would almost not show the video.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
// Register to receive a notification when the movie scaling mode has changed.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieScalingModeDidChange:)
name:MPMoviePlayerScalingModeDidChangeNotification
object:nil];
kDomain = [NSString stringWithString:#"http://www.virtua-book.com/"];
[navigationController setNavigationBarHidden:YES];
NSURL *ur=[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:#"IPAD" ofType:#"mp4"]];
mpMCtr=[[MPMoviePlayerController alloc] initWithContentURL:ur];
mpMCtr.fullscreen=YES;
[mpMCtr setScalingMode:MPMovieScalingModeFill];
[mpMCtr setShouldAutoplay:YES];
[mpMCtr setControlStyle:MPMovieControlStyleNone];
[mpMCtr setMovieSourceType:MPMovieSourceTypeFile];
mpMCtr.view.frame = CGRectMake(0, 0, 1024, 768);
[mpMCtr setRepeatMode:MPMovieRepeatModeNone];
[mpMCtr play];
[ur release];
// Override point for customization after app launch
[navigationController.view addSubview:mpMCtr.view];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
return YES;
}
// Notification called when the movie finished playing.
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[mpMCtr.view removeFromSuperview];
}
To fix back/forward (or previous/next) buttons you should do the following:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
...
- (void) moviePlayerPlaybackStateDidChange: (NSNotification *) notification {
if (moviePlayer.playbackState == MPMoviePlaybackStateStopped) {
[moviePlayer setContentURL:[moviePlayer contentURL]];
[moviePlayer play];
}
}
Something along these lines is probably what you want to do:
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentUrl:movieUrl];
[self presentMoviePlayerViewController:mpvc];
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc]initWithContentURL:movieURL];
mp.moviePlayer.controlStyle = 2;
Ok, guys, I found that this: is deprecated.
The solution is multimediaPlayer.controlStyle = MPMovieControlStyleDefault; but it still doesn't work.