2009-08-31 24 views
5

的頂部我經歷的例子就從蘋果疊加在流媒體的MPMoviePlayerController

我試着去覆蓋上的MPMoviePlayerController的頂部「在iPhone MoviePlayer」,

它完美的作品與視頻剪輯是捆綁,

但它不會工作,如果我從URL流視頻。

覆蓋視圖會隱藏在玩家後面。

有沒有辦法讓重疊視圖前面?

回答

11

MPMoviePlayerController創建自己的窗口並將其設置爲關鍵窗口 - 您可能已經從MoviePlayer示例應用程序中知道這一點。

我不知道爲什麼,但是播放器使用流時有延遲 - 所以剛剛初始化播放器後得到的keyWindow可能不是播放器的窗口,因爲它似乎稍後會添加。

可以「欺騙」,並用一個定時器,幾秒鐘後讓播放器窗口,並添加您的疊加:

[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(addMyOverlay:) userInfo:nil repeats:FALSE] 

或者你可以監聽UIWindowDidBecomeKeyNotification事件,並執行相同的:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyWindowChanged:) name:UIWindowDidBecomeKeyNotification object:nil]; 

這兩個選項都不錯(我很想知道一個更乾淨的方式來做到這一點),但它完成了工作。

+0

你是男人!你已經解決了我的問題!非常感謝 – vicky 2009-09-07 08:06:35

+3

實際上我使用了UIWindowDidBecomeKeyNotification而不是定時器,它完美地工作。不需要定時器。 – vicky 2009-10-22 00:16:00

+0

好的,我同意。你的窗口 - 解決方案是完美的。 – 2009-10-23 23:11:17

1

先前的答案是基於計時器。 &固定5秒。

當電影播放器​​開始時,會嚮應用程序添加一個新窗口。

使用計時器檢查天氣是否將新窗口添加到應用程序中。

添加窗口(電影播放器​​窗口)時。設置通知。

-(void)viewDidLoad{ 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(moviePreloadDidFinish:) 
               name:MPMoviePlayerContentPreloadDidFinishNotification 
               object:nil]; 

    // Register to receive a notification when the movie has finished playing. 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(moviePlayBackDidFinish:) 
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:nil]; 

    // Register to receive a notification when the movie scaling mode has changed. 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(movieScalingModeDidChange:) 
               name:MPMoviePlayerScalingModeDidChangeNotification 
               object:nil]; 
    videoListController.xmlClassVideoList=t; 
    // here ttttt is a timer declared in .h file 
    tttttt=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self  selector:@selector(startMy) userInfo:nil repeats:YES]; 
} 

-(void)startMy{ 
    NSArray *windows = [[UIApplication sharedApplication] windows]; 
    NSLog(@"%i",[windows count]); 
    // depends on your application window 
    // it may be 1/2/3 
    if ([windows count] > 3) { 
     // Locate the movie player window 
     [tttttt invalidate]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyWindowChanged:) name:UIWindowDidBecomeKeyNotification object:nil]; 
    } 
} 
+2

我同意每0.5秒檢查比等待5秒更有意義,但爲什麼不從一開始就聽取UIWindowDidBecomeKeyNotification,並忘記計時器? – 2009-10-20 20:57:55

3

當您收到「MPMoviePlayerContentPreloadDidFinishNotification」通知時,您可以覆蓋您的視圖。

註冊通知:

// Notification called when the movie finished preloading. 
- (void) moviePreloadDidFinish:(NSNotification*)notification 
{ 
    NSArray *windows = [[UIApplication sharedApplication] windows]; 
    if ([windows count] > 1) 
    { 
     // Locate the movie player window 
     UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow]; 
     if ([moviePlayerWindow viewWithTag:0x3939] == nil) { 
      self.videoOverlayView.tag = 0x3939; 
      [moviePlayerWindow addSubview:self.videoOverlayView]; 
     } 
     [moviePlayerWindow bringSubviewToFront:self.videoOverlayView]; 
    } 
} 
2

一個非常簡單的解決方案:

appDelegate.window.backgroundColor = [UIColor clearColor]; 
appDelegate.window.windowLevel = 2; 

這將在保持您的應用程序UI

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(moviePreloadDidFinish:) 
              name:MPMoviePlayerContentPreloadDidFinishNotification 
              object:nil]; 

收到通知時添加覆蓋圖視頻窗口的頂部。

my post