I have used a MPMoviePlayerController object to play a movie in my app but as soon as I call [moviePlayer play] in a function there is a small jerk and then the movie starts playing.
Edit:
This is the code that I am using.
NSString *urlStr = [[NSBundle mainBundle] pathForResource:sVideoName ofType:nil];
NSURL *movieURL = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.view.frame = CGRectMake(lowerScroll.frame.size.width *(((AppDelegate *)[UIApplication sharedApplication].delegate).currentPage-1), 0.0,1024.0,lowerScroll.frame.size.height);
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.scalingMode = MPMovieScalingModeFill;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.view.backgroundColor = [UIColor clearColor];
moviePlayer.view.alpha = 0;
[lowerScroll addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[self performSelector:#selector(showMovie) withObject:nil afterDelay:0.3];
- (void)showMovie {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[moviePlayer play];
moviePlayer.view.alpha = 1;
[UIView commitAnimations];
}
- (void)movieFinishedCallback:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
if (moviePlayer) {
[moviePlayer stop];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
moviePlayer = nil;
}
}
Could anybody please tell what could be the reason for the same and how to remove that jerk.
Thanx in advance.
Related
I am using the MPMoviePlayerController to open a video file.The video runs fine.But suppose if the video file is 10 sec ,with 1 sec remaining the video stops.Is that its natural way of playing or should we specify something.The follow is the code used
NSURL *fileURL = [NSURL URLWithString:location];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
self.moviePlayerController.controlStyle = MPMovieControlStyleFullscreen;
self.moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController.view setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:self.moviePlayerController.view];
- (void)moviePlaybackComplete:(NSNotification *)notification {
NSNumber *finishReason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded) {
self.moviePlayerController = [notification object];
self.moviePlayerController.view.hidden = YES;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view removeFromSuperview];
[self.moviePlayerController release];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
I want to hide the controls from the MPMoviePlayer with this code:
-(IBAction)video:(id)sender {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Intro" ofType:#"mov"];
NSURL *movie = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *control = [[MPMoviePlayerController alloc]initWithContentURL:movie];
//[self.view addSubview: control.view];
control.scalingMode = MPMovieScalingModeFill;
control.controlStyle = MPMovieControlStyleNone;
control.shouldAutoplay = YES;
[control play];
MPMoviePlayerViewController *movieplayer = [[MPMoviePlayerViewController alloc]initWithContentURL:movie];
[self presentMoviePlayerViewControllerAnimated:movieplayer]; }
But that does not work.
You are repeating code. MPMoviePlayerViewController has MPMoviePlayerController. So use it as movieplayervc.moviePlayer.controlStyle = MPMovieControlStyleNone;
have you tried this
[videoPlayerobj setControlStyle:MPMovieControlStyleNone];
My player is set up in the viewDidLoad and this line hides the MPMoviePlayerController. I have intialised my MPMoviePlayer controller as *stream.
stream.view.hidden = YES;
Hope this helps!
You can play video and stop video and remove from your custom view with this code. and MPMoviePlayerController is movie player.
Hope this is useful for you.thank you
-(void)playMovie:(id)sender {
UIButton *buttonThatWasPressed = (UIButton *)sender;
buttonThatWasPressed.enabled = NO;
NSString * str=[[NSBundle mainBundle]pathForResource:#"yo2" ofType:#"mov"];
NSURL * url=[NSURL fileURLWithPath:str];
MPMoviePlayerController * movieController=[[MPMoviePlayerController alloc]initWithContentURL:url];
movieController.controlStyle=MPMovieControlStyleFullscreen;
[movieController.view setFrame:self.view.bounds];
[self.view addSubview:movieController.view];
[movieController prepareToPlay];
[movieController play];
_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 = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES]; }
This method is called when your video or movie is stop from user or video playback has finish.
-(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;
[self dismissViewControllerAnimated:YES
completion:^{
[self performSegueWithIdentifier:#"show" sender:self];
}];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification { // Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissViewControllerAnimated:YES
completion:^{
[self performSegueWithIdentifier:#"show" sender:self];
}];
}
I am am working on my app.which have requirement play video on iPhone by server. I have a video link http://www.cwtmedia.se/cwtvideo.mp4 . Can any body suggest me how i perform this on MPMoviePlayerController.I am using this code for that but its not working.
enter code here
NSURL *url = [NSURL fileURLWithPath:#"http://www.cwtmedia.se/cwtvideo.mp4"];
moviePlayer1 = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer1.view];
moviePlayer1.view.frame = CGRectMake(0, 0, 320, 416);
moviePlayer1.fullscreen=YES;
[moviePlayer1 setFullscreen:NO animated:YES];
moviePlayer1.controlStyle = MPMovieControlStyleFullscreen;
[moviePlayer1 play];
by the way here's how i use mpmovieplayercontroller for streaming :
NSURL *url = [NSURL URLWithString:videoUrl];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer setControlStyle:MPMovieControlStyleDefault];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
CGRect frame;
if(self.interfaceOrientation ==UIInterfaceOrientationPortrait)
frame = CGRectMake(20, 69, 280, 170);
else if(self.interfaceOrientation ==UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation ==UIInterfaceOrientationLandscapeRight)
frame = CGRectMake(20, 61, 210, 170);
[moviePlayer.view setFrame:frame]; // player's frame must match parent's
[self.view addSubview: moviePlayer.view];
[self.view bringSubviewToFront:moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer prepareToPlay];
[moviePlayer play];
and then here's the delegate method :
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player respondsToSelector:#selector(setFullscreen:animated:)]){
//self.navigationController.navigationBarHidden = YES;
[player.view removeFromSuperview];
}
}
hope this will help you..
As far as I know you have 2 options:
1) First download the file and play it locally. Like this:
NSString *url = [[NSBundle mainBundle] pathForResource:#"cwtvideo" ofType:#"mp4"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
2) Use the HTTP streaming protocol. As far as I know HTTP streaming is the only streaming protocol known by the MPMoviePlayerController.
Hope this helps.
Cheers!
I am playing a video by using MPMoviePlayerController:
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"myvideo.MOV" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0,0, 1024, 675);
[moviePlayer play];
But requirement is that by default video should be in full screen and when I minimize it should be in above frame size.
Please help me out.
Try this.
#define degreesToRadian(x) (M_PI * (x) / 180.0)
-(void)playMovieAtURL:(NSURL*)movieURL
{
if ([NSClassFromString(#"MPMoviePlayerController") instancesRespondToSelector:#selector(view)])
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
// running iOS 3.2 or better
mViewPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
mViewPlayer.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
CGRect newFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
mViewPlayer.view.frame = newFrame;
CGAffineTransform landscapeTransform;
landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
[mViewPlayer.view setTransform: landscapeTransform];
[self.view addSubview:mViewPlayer.view];
[mViewPlayer.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieDidExitFullscreen:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[mViewPlayer moviePlayer]];
#endif
}
else
{
MPMoviePlayerController *mMPPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
mMPPlayer.scalingMode=MPMovieScalingModeFill;
mMPPlayer.backgroundColor=[UIColor blackColor];
[mMPPlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mMPPlayer];
}
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) movieDidExitFullscreen:(NSNotification*)notification
{
//[[UIApplication sharedApplication] setStatusBarHidden:YES];
/*/ Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissModalViewControllerAnimated:YES];*/
[[NSNotificationCenter defaultCenter] removeObserver: self
name:MPMoviePlayerPlaybackDidFinishNotification
object: [notification object]];
MPMoviePlayerController *theMovie1 = [notification object];
[theMovie1.view removeFromSuperview];
[theMovie1 release];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
//[[UIApplication sharedApplication] setStatusBarHidden:YES];
/*/ Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissModalViewControllerAnimated:YES];*/
[[NSNotificationCenter defaultCenter] removeObserver: self
name:MPMoviePlayerPlaybackDidFinishNotification
object: [notification object]];
MPMoviePlayerController *theMovie1 = [notification object];
[theMovie1.view removeFromSuperview];
[theMovie1 release];
}
I am running an intro movie in a MPMoviePlayerController and i've got a problem with the allocated memory not being released.
When the movie finishes, Instruments shows the memory being released.
However when i skip the movie by tapping on it the memory is not released.
Here are the important parts of my code:
- (void)applicationDidFinishLaunching:(UIApplication *)theApplication {
self.application = theApplication;
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"mymovie.mov" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[window addSubview:moviePlayer.view];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.view.frame = window.frame;
UIView *introFrontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
introFrontView.userInteractionEnabled = YES;
introFrontView.opaque = NO;
introTabRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(introTabFrom:)];
[introFrontView addGestureRecognizer:introTabRecognizer];
[moviePlayer.view addSubview:introFrontView];
[introFrontView release];
[introTabRecognizer release];
self.moviePlayerController = moviePlayer;
[moviePlayer release];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(introFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController play];
}
- (void)introTabFrom:(UITapGestureRecognizer*)recognizer{
[[NSNotificationCenter defaultCenter] postNotificationName:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
}
- (void) introFinished:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController pause];
[self.moviePlayerController stop];
[self.moviePlayerController release];
...
}
Is there anything i forgot? To my understanding i'm posting the correct notification...
Any ideas?
You may want to register for MPMoviePlayerPlaybackStateDidChangeNotification, and look at the playbackState that is passed in.