Overlay on top of Streaming MPMoviePlayerController

12,451

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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];
    }
}
Share:
12,451
vicky
Author by

vicky

Updated on June 04, 2022

Comments

  • vicky
    vicky about 2 years

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

    Im trying to overlay on top of the 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?

  • alex_c
    alex_c over 14 years
    I agree that checking every 0.5s makes more sense than waiting 5s, but why not just listen for UIWindowDidBecomeKeyNotification from the beginning, and forget the timer?
  • vicky
    vicky over 14 years
    actually i used the UIWindowDidBecomeKeyNotification rather than timer, it works perfectly. no timer needed.
  • Sagar Kothari
    Sagar Kothari over 14 years
    Ok I agree. That your window - solution is perfect.
  • occulus
    occulus about 13 years
    I've tried both notification centre and timer-based approach. In both cases my overlay appears, but with the wrong rotation -- i.e. in landscape mode (which the pre3.2 movie player appears), the overlay is appearing on the screen as if I'm still in portrait mode. Anyone have any experience with that?