按下时MPMoviePlayer关闭

时间:2010-02-22 21:29:48

标签: iphone objective-c mpmovieplayer dismiss

我有一个MPMoviePlayer设置来播放我的应用程序的介绍电影。这很有效,唯一的问题是它持续14秒,我想让我的用户有机会通过按电影上的任何地方跳过介绍。

我隐藏了电影控件,因为它们不需要。

代码:

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

谢谢!

1 个答案:

答案 0 :(得分:2)

编辑:我的初始解决方案不起作用,因为电影显示在第二个窗口中,分层放在应用程序主窗口的顶部(iPhone上的视图层次结构中很少有一个窗口)。此解决方案基于Apple's MoviePlayer sample code,确实有效:

. . .
    // 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];
}

注意:您必须在实例变量中保存对电影播放器​​的引用,并且最方便的是声明我们可以用来访问它的属性。这就是为什么在示例中使用self.intro而不仅仅是intro。如果您不知道如何声明实例变量和属性,则此站点和其他地方有大量信息。

****以下的原始答案

(在这种情况下不起作用,但在很多类似的情况下,所以我会把它留作警告和/或鼓舞人心的例子。)

。 。 。如果没有其他工作,我建议继承子UIWindow,并确保您的应用程序委托实例化而不是正常的UIWindow。您可以拦截该类中的触摸并直接发送通知或取消电影(如果您已在窗口子类的ivar中存储指向MPMoviePlayer的指针)。

@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