I'm trying to prevent the transition animation that occurs when you exit a video in MPMoviePLayerController when the user presses done. I can stop it fine when the movie finishes on its own using the moviePlayBackDidFinish: notification but for some reason when I try it in the exact same way with the exitedFullscreen: notification (which responds to a done press) it doesn't work, as in, the animation occurs.
Here is the complete code, any help would be much appreciated.
-(void) playvideo
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"test" ofType:#"MOV"]];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height);
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
moviePlayer.fullscreen = YES;
}
-(void)moviePlayBackDidFinish: (NSNotification*)notification
{
moviePlayer.fullscreen = NO;
[moviePlayer.view removeFromSuperview];
}
- (void)exitedFullscreen:(NSNotification*)notification {
moviePlayer.fullscreen = NO;
[moviePlayer.view removeFromSuperview];
}
you can to remove animation using this code .
-(void)moviePlayBackDidFinish: (NSNotification*)notification
{
[moviePlayer stop];
[moviePlayerController.view removeFromSuperview];
moviePlayer = nil;
}
- (void)exitedFullscreen:(NSNotification*)notification {
[moviePlayer stop];
[moviePlayerController.view removeFromSuperview];
moviePlayer = nil;
}
Related
The problem video can be seen in :
http://app.bowerchat.com/images/118_1435046739658.mp4
MPMoviePlayerViewController Dismissing immediately without play.
What can be reason?Some videos are loaded nicely and playing.
How can i check the video can be played or not?
I tried
NSLog(#"%lu",(unsigned long)self.mpController.moviePlayer.loadState);
NSLog(#"%d",self.mpController.moviePlayer == nil);
Results always 0 - 0
MY Code to play video
self.mpController = [[MPMoviePlayerViewController alloc] initWithContentURL:moveUrl];
[self.mpController.moviePlayer prepareToPlay];
NSLog(#"%lu",(unsigned long)self.mpController.moviePlayer.loadState);
NSLog(#"%d",self.mpController.moviePlayer == nil);
[parent presentMoviePlayerViewControllerAnimated:self.mpController];
The reason could be that movie that has extention *.mp4 is actually of different type.
Also try to add the following:
self.mpController.moviePlayer.shouldAutoplay = YES;
[self.mpController.moviePlayer play];
Try this snippet :
-(void)setupMovie
{
NSURL *movieURL = [NSURL fileURLWithPath:movieURL];
self.mpController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self. mpController.scalingMode = MPMovieScalingModeAspectFill;
self. mpController.controlStyle = MPMovieControlStyleNone;
self.mpController.view.frame = self.view.frame;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateDidChange:) name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification object:nil];
[self.view addSubview:self.mpController.view];
[self.mpController prepareToPlay];
}
- (void)moviePlayBackDidFinish:(MPMoviePlayerController *)player
{
[self.mpController.view removeFromSuperview];
}
- (void)moviePlayerPlaybackStateDidChange:(NSNotification*)notification
{
if (self. mpController.isPreparedToPlay) {
[self. mpController play];
}
}
And make sure movieURL is correct and video exist by this provided url
I want to play video automatically without on click of play button.
So i found a property of MPMovieplayerController "BOOl Shouldautoplay"
But don't know how to use it.
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:fileName]];
moviePlayer.initialPlaybackTime = 0;
moviePlayer.view.frame = self.view.frame ;
[self.view addSubview:moviePlayer.view] ;
moviePlayer.shouldAutoplay = YES;
[moviePlayer prepareToPlay];
Hope it helps
this is another way
-(void) mpVideo:(NSString *)fileName {
videoFileName=fileName;
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:fileName]];
moviePlayer.initialPlaybackTime = 0;
moviePlayer.view.frame = self.view.frame ;
[orientationHandler VideoStart];
[self.view addSubview:moviePlayer.view] ;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.shouldAutoplay = NO;
[moviePlayer prepareToPlay];
}
-(void)moviePlaybackDidFinish:(NSNotification *)notification {
// your custom code which you want to play after the player did finish playing
}
I have a view within my view controller and have it declared as a property _movieSubview. I add a MPMoviePlayerViewController to _movieSubview and the video plays nicely.
-(void)moviePlay{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"High Rope" ofType:#"mov"]];
_playerController=[[MPMoviePlayerViewController alloc]initWithContentURL:url];
//[self presentMoviePlayerViewControllerAnimated:_playerController];
_playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[_playerController.moviePlayer prepareToPlay];
_playerController.moviePlayer.repeatMode = MPMovieRepeatModeOne;
_playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
_playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[_playerController.moviePlayer play];
/*[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_playerController];*/
//---play partial screen---
_playerController.view.frame = CGRectMake(0, 0, 320, 460);
[_movieSubview addSubview:_playerController.view];
}
- (void)moviePlayerDidFinish:(NSNotification *)note
{
if (note.object == _playerController) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded)
{
[_playerController.moviePlayer play];
}
}
}
My 2 questions are:
My status bar disappears when I add the MPMoviePlayerViewController inside my subview. How do I keep the status bar?
How can I have the video play twice on repeat, then wait for user to play after that?
You should present _playerController instead of adding as subview. Use this code while creating the instance of MPMoviePlayer:
[moviePlayer setFullscreen:YES animated:YES];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:#selector(moviePlayerEvent:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
[notificationCenter addObserver:self selector:#selector(playBackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
-(void)moviePlayerEvent:(NSNotification*)aNotification{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
}
-(void)playBackStateChanged:(id)sender{
MPMoviePlaybackState playbackState = [moviePlayerViewController.moviePlayer playbackState];
switch (playbackState) {
case MPMoviePlaybackStateStopped :
//play again
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
break;
case MPMoviePlaybackStatePlaying :
break;
case MPMoviePlaybackStateInterrupted :
break;
}
}
i click on the button here is the code
//NSURL *url = [NSURL URLWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
_moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:self.urlFile];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
_moviePlayer.view.frame = CGRectMake(0, 0, 320, 300);
[_moviePlayer setFullscreen:YES animated:YES];
and it plays but when i press the Done button, here is the method
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
player.initialPlaybackTime = -1;
[player.view removeFromSuperview];
}
it looks like this
sorry dont have enough to post image
it has the black lining on the bottom and the play button disappears as well, how do i load it back to the point it was when the cell was pressed and came to the detailViewController
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];