在Streaming MPMoviePlayerController上叠加

时间:2009-08-31 08:59:11

标签: iphone overlay mpmovieplayercontroller

我从苹果“iPhone上的MoviePlayer”中看到了这个例子

我试图覆盖在mpmovieplayercontroller上,

它与捆绑的视频剪辑完美配合,

但如果我从网址流式传输视频,它就无法工作。

叠加视图将隐藏在播放器后面。

有没有办法将叠加视图放到前面?

4 个答案:

答案 0 :(得分: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];

这两种选择都不是很好(我很想知道一种更清洁的方法)但是它完成了工作。

答案 1 :(得分:3)

当您收到“MPMoviePlayerContentPreloadDidFinishNotification”通知时,您可以覆盖您的视图。

注册通知:

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

收到通知后添加叠加层视图:

//  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 :(得分:2)

一个非常简单的解决方案:

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

这会将您的应用UI保持在视频窗口的顶部。

my post

答案 3 :(得分: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];
    }
}