ioscocoa-touchmpmovieplayercontrollermpmovieplayermpmoviewcontroller

How to add a title over MPMovieplayerViewController and hide it on tap


I need to add a title over the top toolbar in MPMoviePlayerViewController, and if am playing a video, a user tap should hide the title just like it hide any other controls.

Currently I am adding a UILabel as a subview of moviePlayer view. Though this approach adds a title over the topbar (I am setting the frame accordingly), it does not hide the title when user taps over the screen.

Is there any direct api/hack through which I can get access to the top toolbar of MPMoviePlayerViewController? I am thinking like, if I can add the title as a subView of top toolbar, hiding process will be handled by MPMoviePlayerViewController. Any thoughts?

Thanks in advance!


Solution

  • Note that my answer is referring to MPMoviePlayerController, not MPMoviePlayerViewController. I would advise you to use it either directly or via its reference on MPMoviePlayerViewController.

    First of all, never ever add anything directly onto MPMoviePlayerController's view. That is prone to have all kinds of weird side-effects and is clearly advised against in the documentation. Instead use its backgroundView for hosting your custom stuff or the parent of MPMoviePlayerController's view (making it a sibling).

    But then again, that won't solve the issue you describe with having that view/label/whatever dis/reappear together with the controls if the user taps or simply waits.

    There are ways to do that, but I am afraid those are in the grey-zone - or in other words, have a chance of getting rejected by Apple. In fact its not just a grey-zone but a clear violation of Apple's development guidelines.

    Just, I have used this trick in the past on major apps and never got detected/rejected.

    First, you need to locate the interface-view of your MPMoviePlayerController instance.

    /**
     * This quirky hack tries to locate the interface view within the supposingly opaque MPMoviePlayerController
     * view hierachy.
     * @note This has a fat chance of breaking and/or getting rejected by Apple
     *
     * @return interface view reference or nil if none was found
     */
    - (UIView *)interfaceView
    {
        for (UIView *views in [self.player.view subviews])
        {
            for (UIView *subViews in [views subviews])
            {
                for (UIView *controlView in [subViews subviews])
                {
                    if ([controlView isKindOfClass:NSClassFromString(@"MPInlineVideoOverlay")])
                    {
                        return controlView;
                    }
                }
            }
        }
        return nil;
    }
    
    UIView *interfaceView = [self interfaceView];
    

    Now that you got the instance, simply add your view/label/whatever onto that view.

    [interfaceView addSubview:myAwesomeCustomView];