Overlay on top of Streaming MPMoviePlayerController

前端 未结 4 1886
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 03:23

I went through the example from apple \"MoviePlayer on iPhone\"

Im trying to overlay on top of the mpmovieplayercontroller,

it works perfectly with video cli

相关标签:
4条回答
  • 2020-12-13 03:31

    MPMoviePlayerController creates its own window and sets that as the key window - you probably know this already from the MoviePlayer sample app.

    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.

    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:

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

    0 讨论(0)
  • 2020-12-13 03:45

    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.

    my post

    0 讨论(0)
  • 2020-12-13 03:47

    Previous answer was based on timer. & fixed 5 seconds.

    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];
        }
    }
    
    0 讨论(0)
  • You can overlay your view when you receive "MPMoviePlayerContentPreloadDidFinishNotification" notification.

    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];
        }
    }
    
    0 讨论(0)
提交回复
热议问题