views:

1189

answers:

1

Hi.

I have a MPMoviePlayer setup to play an intro movie to my application. That works just great, the only problem is that it lasts for 14 seconds, and I want to give my users a chance to skip the intro by pressing anywhere on the movie.

I have hidden the movie controls, as they are not needed.

Code:

NSString *introPath = [[NSBundle mainBundle] pathForResource:@"intro" ofType:@"mov"];
intro = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:introPath]];
[intro setMovieControlMode:MPMovieControlModeHidden];
[intro play]; 

Thank you!

+2  A: 

EDIT: My initial solution won't work, because the movie is shown in a second window, layered on top of the app's main window (it's very rare to have more than one window in the view hierarchy on the iPhone). This solution, based on Apple's MoviePlayer sample code, does work:

. . .
    // assuming you have prepared your movie player, as in the question
    [self.intro play];

    NSArray* windows = [[UIApplication sharedApplication] windows];
    // There should be more than one window, because the movie plays in its own window
    if ([windows count] > 1)
    {
        // The movie's window is the one that is active
        UIWindow* moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
        // Now we create an invisible control with the same size as the window
        UIControl* overlay = [[[UIControl alloc] initWithFrame:moviePlayerWindow.frame]autorelease];

        // We want to get notified whenever the overlay control is touched
        [overlay addTarget:self action:@selector(movieWindowTouched:) forControlEvents:UIControlEventTouchDown];

        // Add the overlay to the window's subviews
        [moviePlayerWindow addSubview:overlay];
    }
. . .

// This is the method we registered to be called when the movie window is touched
-(void)movieWindowTouched:(UIControl*)sender
{
    [self.intro stop];
}

NB: You must save the reference to the movie player in an instance variable, and it's most convenient to declare a property that we can use to access it. That's why is use self.intro instead of just intro in the example. If you don't know how to declare an instance variable and a property, there is plenty of information on this site and elsewhere.

** ORIGINAL ANSWER BELOW

(Doesn't work in this case, but in many similar scenarios, so I'll leave it as a warning and/or inspiring example.)

. . . if nothing else works I'd recommend subclassing UIWindow and making sure that your app delegate instantiates that instead of a normal UIWindow. You can intercept touches in that class and send a notification or cancel the movie directly (if you've stored a pointer to the MPMoviePlayer in an ivar on your window subclass).

@interface MyWindow : UIWindow {
}
@end

@implementation MyWindow
// All touch events get passed through this method
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
   // The screen has been touched, send a notification or stop the movie
   return [super hitTest:point withEvent:event];
}
@end
Felixyz
Um, I did not understand that..
Emil
That's fine. And if you'd like further clarification, please let me know which part you didn't understand, or which part you did understand, or how familiar you are with UIKit.
Felixyz
I am fairly new to iPhone developing, so I'm not really familiar with UIKit at all.But it's late at night here where I live, so I'll wite some more tomorrow, ok?
Emil
Perfect. We're in the same time zone :)
Felixyz
Ah, ok :pOk, here's the thing. I have got a tab bar app. When it launches, in the AppDelegate files, I play the intro video.How do you implement the code you wrote in that?
Emil
Code: - (void)applicationDidFinishLaunching:(UIApplication *)application { [window addSubview:tabBarController.view]; NSString *introPath = [[NSBundle mainBundle] pathForResource:@"intro" ofType:@"mov"]; intro = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:introPath]]; [intro setMovieControlMode:MPMovieControlModeHidden]; [intro play];}
Emil
@Emil: See edit. Create an instance variable and property in your app delegate, so you can save the pointer to the movie player.
Felixyz
I will try that. Thanks!
Emil
Thank you! That worked perfectly :) I did not have to use the `self.intro` either.
Emil