my question is: I have a short introduction movie for my application and i want to show it when the application launches, the problem is that im not having any success.
Im using this code on the "didFinishLaunchingWithOptions" method:
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"launch" ofType:#"mov"]]];
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.view.frame = _window.bounds;
moviePlayer.scalingMode = MPMovieScalingModeFill;
[_window addSubview:moviePlayer.view];
[_window makeKeyAndVisible];
//[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer play];
Best Regards
What you should do instead is out that code in your RootViewController's viewDidAppear method. Or whatever other view controller is displayed. That kind of thing doesn't belong in the application delegate and you also shouldn't be adding subviews directly to your UIWindow instance.
Put the code in a view controller and add the MPMoviePlayerController's view as a subview to self.view and it should work fine.
Related
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];
}
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];
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
i am trying to play an intro movie (like a splash screen). it used to work fine for sdk 3.0 but now i am doing it for iPad. and its not running movie instead it just show black screen before view appears.
here is my code
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"intro" ofType:#"m4v" inDirectory:nil];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.fullscreen=YES;
moviePlayer.view.frame = CGRectMake(0, 0,1024,768);
[moviePlayer setShouldAutoplay:YES];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer setRepeatMode:MPMovieRepeatModeNone];
[moviePlayer prepareToPlay];
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
return YES;
}
and here is my moviePlaybackDidFinish
- (void) moviePlaybackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *theMovie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
theMovie.initialPlaybackTime = -1;
[theMovie pause];
[theMovie stop];
[theMovie release];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
i dont know what i am missing here?
thanks for any help....
UPDATE i just found out that it plays sound but no video....is this obvious?pls help
As you're using MPMoviePlayerController over MPMoviePlayerViewController I think you need to be adding the player's view to your view hierarchy before calling play, as per the docs:
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view]; //add the player' view
this might explain why you're hearing audio but not seeing anything, pretty sure you need to setup the app window first too ([window makeKeyAndVisible]; etc)
Now I think I know about the differences between 3.X and 4 with regards the MPMoviePlaybackController and the need to set the view and have it fully working in a child view controller. But despite the following code seeming correct (to me) I still just get a blank screen for the duration of the movie. I know it plays successfully as moviePlayBackDidFinish fires.
Do I need to add it to a modal or similar at this point?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];
player.fullscreen = YES;
player.controlStyle = MPMovieControlStyleNone;
[[player view] setFrame:window.bounds];
[window addSubview: [player view]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
}
the MPMoviePlayerController does NOT have a view property.
you should/must use MPMoviePlayerViewController instead.
here's what I do:
moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:currentChannel.StreamURI]];
[moviePlayerViewController.moviePlayer setControlStyle:MPMovieControlStyleNone];
moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
moviePlayerViewController.moviePlayer.shouldAutoplay = YES;
[moviePlayerViewController.moviePlayer setScalingMode: MPMovieScalingModeAspectFit];
moviePlayerViewController.view.frame = CGRectMake(0, 0, 480, 320);
[self.view addSubview:moviePlayerViewController.view];
note that you might want to change movieSourceType to MPMovieSourceTypeFile or MPMovieSourceTypeUnknown.
The above code is 100% of what I need to play a Movie (in my case a streaming channel)
In my case I had moved the
[window makeKeyAndVisible];
Out from
didFinishLaunchingWithOptions
and into my
movieDidFinish
By putting it back it worked