5

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
vicky
  • 284
  • 2
  • 6
  • 14

4 Answers4

11

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.

alex_c
  • 2,058
  • 2
  • 26
  • 34
  • 3
    actually i used the UIWindowDidBecomeKeyNotification rather than timer, it works perfectly. no timer needed. – vicky Oct 22 '09 at 00:16
  • Ok I agree. That your window - solution is perfect. – sagarkothari Oct 23 '09 at 23:11
  • 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? – occulus Jun 03 '11 at 10:47
3

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];
    }
}
Seunghoon
  • 5,632
  • 5
  • 35
  • 41
2

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

MaxFish
  • 449
  • 3
  • 7
1

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];
    }
}
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
  • 2
    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? – alex_c Oct 20 '09 at 20:57