简体   繁体   English

在Streaming MPMoviePlayerController上叠加

[英]Overlay on top of Streaming MPMoviePlayerController

I went through the example from apple "MoviePlayer on iPhone" 我通过苹果“iPhone上的MoviePlayer”的例子

Im trying to overlay on top of the mpmovieplayercontroller, 我试图覆盖在mpmovieplayercontroller之上,

it works perfectly with video clip that is in bundle, 它与捆绑的视频剪辑完美配合,

but it wont work if i stream the video from the url. 但如果我从网址流式传输视频,它就无法工作。

the overlay view will just get hide behind the player. 叠加视图将隐藏在播放器后面。

is there a way to bring the overlay view up to front? 有没有办法将叠加视图放到前面?

MPMoviePlayerController creates its own window and sets that as the key window - you probably know this already from the MoviePlayer sample app. MPMoviePlayerController创建自己的窗口并将其设置为关键窗口 - 您可能已经从MoviePlayer示例应用程序中知道这一点。

I don't know why, but there's a delay when the player uses a stream - so the keyWindow you get right after you initialize the player is likely not the player's window, since that seems to get added later. 我不知道为什么,但是当玩家使用流时会出现延迟 - 所以你在初始化玩家后得到的keyWindow可能不是玩家的窗口,因为这似乎是后来添加的。

You can "cheat" and use a timer to get the player window a few seconds later, and add your overlay: 您可以“欺骗”并使用计时器在几秒钟后获取播放器窗口,并添加叠加层:

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

Or you can listen for the UIWindowDidBecomeKeyNotification event, and do the same: 或者您可以侦听UIWindowDidBecomeKeyNotification事件,并执行相同的操作:

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

Neither option is great (I'd love to know a cleaner way to do this), but it gets the job done. 这两种选择都不是很好(我很想知道一种更简洁的方法),但它完成了工作。

You can overlay your view when you receive "MPMoviePlayerContentPreloadDidFinishNotification" notification. 收到“MPMoviePlayerContentPreloadDidFinishNotification”通知时,您可以覆盖视图。

Register for the notification: 注册通知:

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

Add overlay view when receiving the notification: 收到通知时添加叠加视图:

//  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];
    }
}

A very simple solution: 非常简单的解决方案:

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

This will keep your app UI on top of the video window. 这会将您的应用UI保持在视频窗口的顶部。

my post 我的帖子

Previous answer was based on timer. 以前的答案是基于计时器。 & fixed 5 seconds. 并固定5秒。

When Movie player begins, a new window is added to application. 当电影播放器​​开始时,会向应用程序添加一个新窗口。

Use a timer to check weather a new window is added to your application or not. 使用计时器检查天气是否将新窗口添加到您的应用程序中。

When a window ( movie player window ) is added. 添加窗口(电影播放器​​窗口)时。 set notifications. 设置通知。

-(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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM