MPMoviePlayer 按下时关闭

发布于 2024-08-22 22:45:39 字数 418 浏览 6 评论 0原文

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

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

代码:

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

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世态炎凉 2024-08-29 22:45:39

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

. . .
    // 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文