2010-02-22 54 views
1

我有一個MPMoviePlayer設置來播放我的應用程序的開場電影。這很好,唯一的問題是它會持續14秒,我想讓用戶有機會通過按下電影上的任何位置來跳過前奏。MPMoviePlayer在按下時關閉

我隱藏了電影控件,因爲它們不是必需的。

代碼:

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

謝謝!

回答

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。如果你不知道如何聲明一個實例變量和一個屬性,這個網站和其他地方有很多信息。

**** ORIGINAL回答以下

(在這種情況下不工作,但在許多類似的情況,所以我會離開它作爲一個警告和/或鼓舞人心的例子。)

。 。 。如果沒有別的工作,我會建議子類化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 
+0

恩,我不明白.. – Emil 2010-02-23 10:54:20

+0

這很好。如果您想進一步澄清,請告訴我您不瞭解哪部分,或者您瞭解了哪部分,或者您對UIKit有多熟悉。 – Felixyz 2010-02-23 14:33:39

+0

我對iPhone開發相當陌生,所以我對UIKit根本不熟悉。 但是,在我住的地方深夜,所以明天再打個招呼,好嗎? – Emil 2010-02-23 22:34:16

相關問題