MPMoviePlayerController problem at start - objective-c

I have a problem with MPMoviePlayerController, because when I start it in fullscreen it exists, but the song is still playing. I added the MPMoviePlayerDidExitFullscreenNotification notification and it says that when the video starts playin it exists the full screen. Here's my code:
_multimediaPlayer = [[MPMoviePlayerController alloc] init];
_multimediaPlayer.controlStyle = MPMovieControlStyleDefault;
_multimediaPlayer.initialPlaybackTime = 0;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlayerDidExitFullscreen:)
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlayerPlaybackStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];
NSString *path = [NSString stringWithFormat:#"%#/mag_%d/%#", [FMUtils documentsFolderPathWithFile:nil], _magID, _pageObject.fileName];
if ([FMUtils fileExistsAtPath:path]) {
_multimediaPlayer.contentURL = [NSURL fileURLWithPath:path];
} else {
_multimediaPlayer.contentURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#", self.dataURL, _pageObject.fileName]];
}
CGSize objectViewSize = self.frame.size;
_multimediaPlayer.view.frame = CGRectMake(0, 0, objectViewSize.width, objectViewSize.height);
[self addSubview:_multimediaPlayer.view];
if (_pageObject.blink) {
[_multimediaPlayer setFullscreen:YES animated:YES];
}
[_multimediaPlayer play];
Most of the time it happens when the video is not downloaded and it needs to stream it.
Do you have any idea why is this happen?

Problem solved while I not listen to MPMoviePlayerPlaybackStateDidChangeNotification.

Related

why isn't removeFromSuperview removing my movie subview? Xcode

I'm trying to get a movie that plays to dismiss on its own without having to hit the "Done" button. I think it's a recent iOS 6 problem since I'm following a tutorial exactly and when I insert NSLogs the NSNotification and removeFromSuperview are being recognized but the movie stays there once it has ended. Here's my code, please help:
- (IBAction)playMovie:(id)sender
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"RomneyFlipSequence1" ofType:#"mov"]];
_moviePlayer =
[[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:NO];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
#end
You entered the player into fullscreen mode.
[_moviePlayer setFullscreen:YES animated:NO];
Reading other solutions in SO it seems that when you press "Done" the player is taken out of fullscreen first, and then the notification is thrown. After reading this answer, adding
[_moviePlayer setFullscreen:NO animated:YES];
before your removeFromSuperview call will solve your problem.
If the above doesn't work, in addition you may try stoping the player first so the full code will be
-(void)removePlayer:(MPMoviePlayerController *)player{
NSLog(#"Playback Finished");
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:_moviePlayer];
[_moviePlayer stop]; // <-- May not be needed
if ([_moviePlayer respondsToSelector:#selector(setFullscreen:animated:)])
{
[_moviePlayer setFullscreen:NO animated:YES];
[_moviePlayer.view removeFromSuperview];
}
_moviePlayer=nil;
}
I use the same method for both notifications "Done" and "PlayBackFinished"
Hope this helps.
You had added _moviePlayer.view as a view and you are trying to remove player.view
i think this will do it :
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:#selector(setFullscreen:animated:)])
{
[_moviePlayer.view removeFromSuperview];
}
}

memory leak during release of movieplayer

In one of my apps i want to play a video file from local.so iam using the below code for playing the file.
-(IBAction)playMovie{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"Myvideo" ofType:#"mov"]];
MPMoviePlayerController *moviePlayer=[[[MPMoviePlayerController alloc]initWithContentURL:url]autorelease];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
//[moviePlayer release];
}
-(void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
if ([moviePlayer
respondsToSelector:#selector(setFullscreen:animated:)])
{
[moviePlayer.view removeFromSuperview];
}
[moviePlayer release];
}
when i go to analyse or do profile for the above code there shows a warning potential leak happening at 2 places i.e,
[moviePlayer setFullscreen:YES animated:YES];
//[moviePlayer release];
one more is leak is at this part
{
[moviePlayer.view removeFromSuperview];
}
[moviePlayer release]
so how to avoid this memory leak.Even i tried giving autorelease the NSURL Line in the begining of the code,when i do autorelease leak wont happen but video wont play.so how to solve this issue can anyone help??
try this in moviePlayBackDidFinish method, may be it will help
if (videoPlayer) {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayer];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification object:videoPlayer];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMovieMediaTypesAvailableNotification object:videoPlayer];
[videoPlayer pause];
videoPlayer.initialPlaybackTime = -1;
[videoPlayer stop];
[videoPlayer.view removeFromSuperview];
[videoPlayer release];
videoPlayer = nil;
}

How to play a video with MPMoviePlayerController

I'm using this code to play a video streaming from the apple site
- (IBAction)playMovie:(UIButton *)sender {
NSLog(#"start playing");
//NSURL *url = [NSURL URLWithString:#"http://spatie.be/test.mov"];
NSURL *url = [NSURL URLWithString:#"http://stream.qtv.apple.com/events/mar/123pibhargjknawdconwecown/12oihbqeorvfhbpiubqnfv3_650_ref.mov"];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
NSError *error = [[notification userInfo] objectForKey:#"error"];
if (error) {
NSLog(#"Did finish with error: %#", error);
}
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
When playMovie is invoked, moviePlayBackDidFinish is immediatelty called and the error message is logged:
Did finish with error: Error Domain=MediaPlayerErrorDomain Code=-11800
"The operation could not be completed" UserInfo=0x78d25d0
{NSLocalizedDescription=The operation could not be completed}
How can I solve this error?
You didn't tag this as iOS, but MPMoviePlayer is iOS only. This should work just fine with a video that will play on iOS. Unfortunately the video you are trying to play is not iOS compatible. Try this URL:
http://stream.qtv.apple.com/events/mar/123pibhargjknawdconwecown/12oihbqeorvfhbpiubqnfv3_ipad_vod_ref.mov

Problem with video removeFromSuperview

NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"clip" ofType:#"m4v"];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.view.frame = CGRectMake(0, 0,320, 460);
[viewController.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer setShouldAutoplay:YES];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[moviePlayer setMovieControlMode:MPMovieControlModeHidden];
I am playing mp4 file i need to removeFromSuperview when movie is finished.... in notification. is it possible.
Yes it is possible. You need to add Observers to the notifications that the MPMoviePlayer sends (probably in your viewDidLoad):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(exitedFullscreen)
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(finishedPlayback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
then in your methods that are called when the notification fires:
- (void)finishedPlayback:(NSNotification *)notification
{
[self.moviePlayer setFullscreen:NO animated:YES];
}
-(void)exitedFullscreen
{
[self.moviePlayer.view removeFromSuperView];
}

When my movie is finished to play, the app crashes

I tried to launch a movie by clicking in a button. When I click the Movie player appears and works fine. The movie plays until the end and the Movie player disappears. After it disappears, my app crashes...
I use my View Controller in Tab Bar Application with this code :
- (void)moviePlayBackDidFinish:(NSNotification *) aNotification{
MPMoviePlayerController *player = [aNotification object];
[player setFullscreen:NO animated:YES];
[player.view removeFromSuperview];
[player stop];
player.initialPlaybackTime = -1.0;
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player release];
player=nil;
}
- (IBAction)playVideo:(UIButton *)playButton{
NSString *url = [[NSBundle mainBundle] pathForResource:#"Teaser 04" ofType:#"mov"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
player.shouldAutoplay = YES;
player.view.frame = CGRectMake(0., 44., self.view.bounds.size.width, self.view.bounds.size.height-44);
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[self.view addSubview:player.view];
[player play];
}
I don't know what I'm doing wrong. I just want configure a button that start the video and when the video is finished, the MoviePlayer View disappears and the app come back to my initial .xib
Thank you for your help
I found the solution, I don't know if it is the best but for what I want it's working :
- (void)movieFinishedCallBack:(NSNotification *) aNotification{
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player.view removeFromSuperview];
[player stop];
[player release];
}
- (IBAction)playVideo:(UIButton *)playButton{
NSString *url = [[NSBundle mainBundle] pathForResource:#"Teaser 04" ofType:#"mov"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallBack:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
player.view.frame = CGRectMake(0, 0, 950, 600);
[self.view addSubview:player.view];
[player play];
}
I could be wrong, but my guess is that the player gets used somewhere in the backtrace for the call to moviePlayBackDidFinish: ... If I'm right, then deallocing the object there - which it looks like you're doing - is bad news, since the object is still needed somewhere up in the stack.
Check out this post for more details: How to release MPMoviePlayerController?