Change view after playing video - iphone

My Video works when I click my play button, and I know the code for my view change works. I just can't seem to get the view to change after my video is done playing, not sure what is going wrong. Any ideas to get the view to change once the video is done playing?
My code to play movie
-(IBAction)playMovie:(id)sender {
NSString *movieUrl = [[NSBundle mainBundle] pathForResource:#"Movie_1666" ofType:#"m4v"];
playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:movieUrl]];
//Smoothe Transition
//[self presentMoviePlayerViewControllerAnimated:playerController];
//Instant Transistion
[self presentModalViewController:playerController animated:NO];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
[playerController.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:playerController]; }
My method to change views
- (void)playbackFinished:(NSNotification*) notification {
MPMoviePlayerController *playerController = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerController];
View2 *view2 = [[View2 alloc] initWithNibName:#"View2" bundle:nil];
View2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:view2 animated:YES]; }
EDIT:
My Notification object was wrong and wasn't triggering my playbackFinished method
this change fixes that.
- (void)playbackFinished:(NSNotification*) notification {
playerController = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerController];
I also put this in my header file to make it global for use in my playbackFinished method
MPMoviePlayerViewController *playerController;

Once the movie play finished, dismiss the playerController view without animation. Also check, whether you are presenting the View2 in main thread.
Edit:
Dismiss the playerController by
[playerController dismissModalViewControllerAnimated:NO];

Related

how to dismiss MPMoviePlayer ? properly from one ViewController to another

I am using the Notification Center to receive the MPMoviePlayerPlaybackDidFinishNotification, which works as expected.
NSURL * url = [[NSURL alloc] initWithString:#"http://INFINITECREATIVEUNIVERSE.COM/Media/module_1_Dec/c_January27AffactPerfect.mov"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer.view setFrame:CGRectMake(0, 0, 500, 260)];
[self.view addSubview:moviePlayer.view];
//Some addiontal customization
moviePlayer.fullscreen = YES;
moviePlayer.allowsAirPlay = YES;
moviePlayer.shouldAutoplay = YES;
moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
//moviePlayer.endPlaybackTime = NO;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playMovieFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
The selector does what I would like it to do.
-(void)playMovieFinished:(NSNotification*)theNotification
{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
// Change Segue ID Here
[self performSegueWithIdentifier:#"VC_5" sender:self];
}
My problem occurs when I click next button while the video is still playing. When my next ViewController appears there is no video and I can still hear the audio from the previous ViewController.
My question is:
How do I properly dismiss the View Controller and the MPMoviePlayer.
I am lost.
I downloaded the example MoviePlayer project from Apple, but it is now deprecated and it does not work properly.
I am lost on how to make this work.
I am crossing my fingers that I have asked the question correctly. Please have mercy on my novice question. Thank you robert
use this code
-(void)playMovieFinished:(NSNotification*)theNotification
{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer stop];
// Change Segue ID Here
[self performSegueWithIdentifier:#"VC_5" sender:self];
}

how to play a video on startup of the app or on viewdidload with done button?

I have been able to successfully play a video on start of the app by calling a function that plays the video in the viewDidLoad method. But on doing so, i am unable to see the "Done" button. The "Done" button only seems to appear if i call that same function with the help of a button. I am posting the code below. How will I make this done appear by calling the same function in the viewDidLoad method? What have i been doing wrong? Thank you!
in my viewcontroller.m file
- (void)viewDidLoad
{
[super viewDidLoad];
[self playMedia];
}
- (void) playMedia {
//hide status bar first
[[UIApplication sharedApplication] setStatusBarHidden:YES];
//resets again in playMediaFinished
movieFile = [[NSBundle mainBundle] pathForResource:#"sec" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath: movieFile];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMediaFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
[moviePlayer setFullscreen:NO animated:YES];
[self.view addSubview:moviePlayer.view];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
CGAffineTransform transform = self.view.transform;
[moviePlayer.view setFrame:CGRectMake(0, 0, 480, 320)];
moviePlayer.shouldAutoplay = YES;
NSLog(#"Should be playing Movie");
}
- (void) playMediaFinished: (NSNotification*) theNotification {
moviePlayer = [theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
if ([moviePlayer
respondsToSelector:#selector(setFullscreen:animated:)])
{
[moviePlayer.view removeFromSuperview];
[playerView removeFromSuperview];
//reset status bar
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
NSLog(#"Should be DONE playing Movie");
}
Try doing this using viewDidAppear instead of viewDidLoad. I'm not surprised starting a video in viewDidLoad is causing unexpected behavior considering you're presenting the video before the main view appears on screen. Keep in mind, the view is done loading before it is actually visible.

How to get video player to NOT auto play while using MPMoviePlayerController

I'm trying to code a video into my iPhone app and the code below works perfectly but the only thing is that I rather the video not automatically start playing when the user opens the page. I want them to be able to push the play button. The code below is located in my viewcontroller.m file.
(void)viewDidLoad
{
NSString *url = [[NSBundle mainBundle] pathForResource:#"77860_00_01_MM02_welcome"
ofType:#"mov"];
player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
player.view.frame = CGRectMake(10, 235, 150, 125);
[self.view addSubview:player.view];
[player play];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *moviePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer.view removeFromSuperview];
[player play];
}
Never mind, I figured it out. I just added [player prepareToPlay] under player.shouldAutoplay = NO
Don't put it in your viewDidLoad. Just create an IBAction them link it up to your trigger button.

How do you play a movie in an iphone app?

I want to add a movie at the beginning of my iphone app. I have watched the tutorials and this is the code I have come up with. Why won't it work. I don't even see a movie. I call it from the viewDidLoad function of my ViewController with the following code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"BTS Intro" ofType:#"mov"];
[self playMovieAtURL:[NSURL fileURLWithPath:path
-(void)playMovieAtURL:(NSURL *)theURL {
MPMoviePlayerController *thePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
thePlayer.scalingMode = MPMovieScalingModeAspectFill;
thePlayer.controlStyle = MPMovieControlStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:thePlayer];
[thePlayer play];
}
-(void)myMovieFinishedCallback:(NSNotification *)aNotification {
MPMoviePlayerController *thePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:thePlayer];
[thePlayer release];
}
You initiated playing of a movie but didn't add [thePlayer view] into views hierarchy that is why you do not see anything. Take a look at description of this operation in apple docs:
When you add a movie player’s view to your app’s view hierarchy, be sure to size the frame correctly, as shown here:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];
MPMoviePlayerController Class Reference

MpMoviePlayerViewController closed after setContentURL

I make mpmovieplayerviewcontroller and load movie in it (from stream). Now I have call to setContentURL method that change URL and it works. Whenn app is first time started it shows view with button after button is pressed it opens movie player and load movie. But when I call code where is setContentURL player close it self first, go back to first view where I have to again click play button to again open player (so it remembers and load new URL). How to prevent player to not close it self when switching URL? This must be some small issue, please help :)
This is code when play button is clicked (first view)
-(void)initializeMovieFromStream:(NSString *)var
{
if(player != nil)
{
NSString *title = [[NSString alloc] initWithFormat:#"%#%#%#", #"http://"];
NSURL *nurl = [NSURL URLWithString:title];
NSLog(#"Switching channel...");
[player.moviePlayer setContentURL:nurl];
isMoviePaused = NO;
}
else
{
NSString *title = [[NSString alloc] initWithFormat:#"%#%#%#", #"http://"];
NSURL *nurl = [NSURL URLWithString:title];
player = [[MPMoviePlayerViewController alloc] initWithContentURL:nurl];
[player.moviePlayer.view.window setUserInteractionEnabled:YES];
player.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
player.moviePlayer.controlStyle = MPMovieControlStyleNone;
}
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(movieFinishedPlaying:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[player moviePlayer]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(nowPlayingMovieDidChange:)
name:MPMoviePlayerNowPlayingMovieDidChangeNotification
object:nil];
[self presentModalViewController:player animated:YES];
//[self presentMoviePlayerViewControllerAnimated:player];
UIView *mv = player.view;
//[mv setFrame:CGRectMake(0, 0, 320, 480)];
[mv addSubview:myOverlayChannelPicker];
[mv bringSubviewToFront:myOverlayChannelPicker];
// register this class to observe TestNotification that comes from OverlayChannelPicker
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveTestNotification:)
name:#"TestNotification"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(pauseStreamNotificationHandler:)
name:#"notiPauseStream"
object:nil];
}
-(void) movieFinishedPlaying: (NSNotification *)note
{
[[NSNotificationCenter defaultCenter]
removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:[player moviePlayer]];
//[player release];
}
When URL need to be switch I use this nitification handler
- (void) receiveTestNotification:(NSNotification *) notification
{
NSString *stringFromNote = (NSString *)[notification object];
[switchableChannel setString:stringFromNote];
[self initializeMovieFromStream:stringFromNote];
}
It is solved I just change animation property to NO.