Having a Movie as a transition between two View Controllers - where to put this code? - transition

My whole goal is to have a button on the main view, when you press this button a video plays and when the video is done playing you're now at the second view. The quick video is like a transition between the two VCs. My question is where should I put the 'play movie' code. Here are the scenarios I've tried and the unsatisfying outcomes:
The button in Main VC plays the movie and opens second VC - The movie plays but the second view controller doesn't open, just goes back to the main VC.
The button in Main VC plays the movie and the NSNotification for moviePlayBackDidFinish opens the second VC - same result as above.
The button in Main VC switches to second VC and movie code is in second VC's viewDidLoad (also viewWillLoad, same result) - VC switches but movie doesn't play
The button in Main VC switches to second VC and movie code is in second VC's viewDidAppear - second VC appears then movie plays continuously (not what I want).
What is the best way to set up the code to get the transition effect I'm looking for? Should I make a third View Controller in between the two just to play the movie? Here is the code I'm using to play the movie:
//Sets up the movie file to play
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"TransitionMovie" ofType:#"mov"]];
_moviePlayer = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
//Creates notification observer so it knows when the movie is done playing
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
//Plays the movie
[self presentMoviePlayerViewControllerAnimated:_moviePlayer];

Related

iOS presentMoviePlayerViewControllerAnimated can't be called twice using the same viewController

So I'm trying to present a MPMoviePlayerViewController using the method presentMoviePlayerViewControllerAnimated in my own view controller.
However, once I clicked the blue Done button on the top left, when I present the same viewController again (so that I can resume a video without having to create another instance of MPMoviePlayerViewController), the controls will no longer work if you let them disappear on their own after a few seconds. The controls will still function if you interact with them before they disappear, but once they fade away on their own, you can no longer bring them back up when tapping the video. This happens on both the device and the simulator.
Simplified code:
- (void) startPlayback
{
if (self.videoViewController == nil)
{
self.videoViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:self.videoUrl]];
}
// present the video player
[self presentMoviePlayerViewControllerAnimated:self.videoViewController];
}
assume that startPlayback is a delegate method or something in a view controller, and it's being triggered by a button. Self would refer to the view controller in this case.
So with this, it will bring up the video player which works perfectly. Then, once I wait a bit, the controls will disappear and I can bring them back by tapping the video. I can then dismiss this video player using the Done button on the top left. Once I dismiss it, and I can then bring back the video view controller. The video will still be playing, but now, when I wait a bit and the controls disappear, I can no longer bring up the controls by tapping the screen, effectively trapping myself in the video player.
I have found various solutions into hooking into the Done button, but it hasn't really allowed me to reuse the video player. For example:
// Remove the observer so that the blue button doesn't close the viewController
[[NSNotificationCenter defaultCenter] removeObserver:self.videoViewController name:MPMoviePlayerPlaybackDidFinishNotification object:self.videoViewController.moviePlayer];
// Add itself as an observer
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieEnded:) name:MPMoviePlayerPlaybackDidFinishNotification
object:self.videoViewController.moviePlayer];
In my movieEnded:, I can then dismiss the movie view controller via:
[self dismissMoviePlayerViewControllerAnimated];
But when I try to present the viewController again, the video player just gets stuck Loading... forever.
Any ideas?

Playing subsequent movies in one MPMoviePlayerViewController

I am having a difficulty with the MPMoviePlayViewController.
I insatiate the controller, assign the url and show the player using:
[self presentMoviePlayerViewControllerAnimated:[appDelegate movieController]];
Then when the movie finished I dismiss it:
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self dismissMoviePlayerViewControllerAnimated];
The movie player is alloc in my AppDelegate.
There are some other listeners on MPMoviePlayerPlaybackDidFinishNotification. This is for the case where there might e.g. only be audio and it shows a play/pause button and counters in table cells.
The problem is that when I load a second movie in the same MPMovieViewController, it appears fine but the controls are not working correctly. They are work as long as they are visible, but as soon as they disappear there is no may of getting them back and therefore to dismiss the movie player.Sometimes closing and opening the App works, but sometimes it doesn't and I need to 'kill' the App in order to be able to start again.
Is there a way to play subsequent video's in the MoviePlayer while the controls still work?
Any suggestions how to 'reset' the Player in a way that I can prevent the other listeners from given a DEALLOC as they are listening for the action?
you dont need to dismiss the player you just need to set the new url... or maybe I missunderstood your problem...
Try adding this to your setup movie player controller
moviePlayerController.view.userInteractionEnabled = YES;

View sizing after playing movie in fullscreen mode

I have a fairly standard navigation-based iPhone app I'm developing, where on some screens, there is a button that will play a movie using an MPMoviePlayerController in fullscreen mode. There is a navigation bar at the top of the view. I'm seeing some odd behaviour with the view sizing after the movie plays. Was wondering if anybody else has seen this, and whether there is a fix. Here is what I'm seeing:
I hold the phone in one orientation (portrait or landscape)
I start the movie (it plays in fullscreen mode)
I tap the screen to hide the movie playback controls
I rotate the phone to another orientation (portrait to landscape, or vice-versa)
I tap the screen to bring back playback controls, and then tap the 'Done' button
At this point, the original view comes back on screen, in the new orientation. However, the view is not sized properly to accomodate the status bar at the top of the screen. The status bar obscures part of the navigation bar. Rotating the phone at this point corrects the problem.
If I follow the same sequence, but without hiding the playback controls, everything works just fine, and the original view is sized properly when I dismiss the movie.
Anybody have any clue what might be causing this?
Also, this seems to be pretty easy to reproduce. I'm using Xcode4.2 and the 5.0 SDK. If you create a new iPhone-only project using the "Master-Detail Application" template, and make these changes, you can see the problem happen:
First, add the following code to MasterViewController.m.
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
}
- (void) playStory
{
NSURL *movieURL = [NSURL URLWithString:#"http://double-apps.com/thisisatest.m4v"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
Then, replace the contents of the didSelectRowAtIndexPath method with [self playStory]:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self playStory];
}
That's it, run the app, hit the 'Detail' entry in the main screen, then follow the sequence listed above.

MPMoviePlayerController for iPhone App on iPad

For some reason, the expand button the arrow points to in the screenshot below causes the view controller that initiated video playback to animate back over top of the video, but without stopping video playback which means you can still hear the audio even though the video is no longer visible. I've tried other movie control styles, but there are other problems with those (for example, no controls causes the player to play the entire video before dismissing, i.e. no 'Done' button).
Here is the code that initiates the video playback:
player = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[player setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[[self navigationController] presentModalViewController:player animated:YES];
[[player moviePlayer] play];
Any ideas/suggestions as to how I can either disable that button or receive its notification so I can respond accordingly?
Thanks.
I can't find a solution to this, but I did find a workaround. I simply call [player stop]; in my -viewDidLoad of the calling view controller. The outcome won't be what the user expects when they press that button, but it's better than allowing the video to continue to play when they press it.

When I press "DONE" while playing movie using MPMoviePlayerController which view will get displayed

I am playing movie using MPMoviePlayerController,
I am using TableView,what happening with my application is when I press accessory button it will display detailed view and when I press cell area it will play movie that I wanted,(the way youtube application does)
but when I press "DONE" while playin movie it'll navigate to a view which is not my detail view but some empty view.
Any idea what should I do to achieve it so that when user press "DONE" it will nevigate me to my detail view instead of some empty view.
I tried to push view when "movieFinishedCallback" is executed but it'll pushing one more view on that empty view.
Ideally if your code is correct, pressing the Done button on MPMoviePlayerController object would close the MPMOviePlayerController and return to the controller which invoked it (which in your case is the detail view)
You do not need to do anything in the movieDidFinishCallback except maybe release the movieplayer object.
-(void)myMovieFinishedCallback:(NSNotification*)aNotification {
MPMoviePlayerController* theMovie = [aNotification object];
theMovie.initialPlaybackTime = -1.0;
}