【问题标题】:MPMoviePlayer dismiss when pressedMPMoviePlayer 按下时关闭
【发布时间】:2011-01-19 20:07:09
【问题描述】:

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

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

代码:

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

谢谢!

【问题讨论】:

    标签: iphone objective-c mpmovieplayer dismiss


    【解决方案1】:

    编辑:我的初始解决方案不起作用,因为电影显示在第二个窗口中,位于应用程序主窗口的顶部(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。您可以在该类中拦截触摸并发送通知或直接取消电影(如果您已将指向 MPMoviePlayer 的指针存储在窗口子类的 ivar 中)。

    @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
    

    【讨论】:

    • 没关系。如果你想进一步澄清,请告诉我你不明白的部分,或者你理解的部分,或者你对 UIKit 的熟悉程度。
    • 我对 iPhone 开发还很陌生,所以我对 UIKit 一点也不熟悉。不过我住的地方已经很晚了,所以我明天再写一些,好吗?
    • 完美。我们在同一个时区:)
    • 啊,好的 :p 好的,事情就是这样。我有一个标签栏应用程序。当它启动时,我会在 AppDelegate 文件中播放介绍视频。你如何实现你写的代码?
    • @Emil:见编辑。在您的应用程序委托中创建一个实例变量和属性,以便您可以保存指向电影播放器​​的指针。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多