views:

3937

answers:

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?

+6  A: 

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
You are the man! you have solved my problem! Thanks alot
vicky
actually i used the UIWindowDidBecomeKeyNotification rather than timer, it works perfectly. no timer needed.
vicky
Ok I agree. That your window - solution is perfect.
sugar
+2  A: 

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];
    }
}
sugar
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
A: 

Check it out solution here worked for me:

http://i4iphones.info/index.php?/topic/2347-overlay-view-on-mpmovieplayercontroller/

enea
+2  A: 

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];
    }
}
y5h
+1  A: 

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