Search code examples
iphoneobjective-cmpmovieplayercontrollermpmovieplayer

How to subclass MPMoviePlayer controller


I am not sure if I understand what subclass of 'MoviePlayerController' means. a) Is this when create new view controller and add MPMoviePlayerController instance in it? b) Something else? Some code example would be very helpful.

Thanks


Solution

  • This couldn't be a comment above as it takes up too many characters.

    Okay @1110 I'm going to assume you want to add a UITapGestureRecognizer to the player view, don't forget it already supports pinch gestures for full screen / removing full screen. The code below is assuming you're working with a view controller with MPMoviePlayerController as an iVar.

    You probably don't want to detect a single tap because it already uses tap detection count of 1 in order to show/hide the players controller.

    Below is a code example for you with a gesture recognizer for a double tap

    Code for PlayerViewController.h

    #import <UIKit/UIKit.h>
    #import <MediaPlayer/MediaPlayer.h>
    
    @interface PlayerViewController : UIViewController {}
    
    //iVar
    @property (nonatomic, retain) MPMoviePlayerController *player;
    
    // methods
    - (void)didDetectDoubleTap:(UITapGestureRecognizer *)tap;
    @end
    

    Code for PlayerViewController.m

    #import "PlayerViewController.h"
    
    @implementation PlayerViewController
    @synthesize player;
    
    - (void)dealloc
    {
        [player release];
        [super dealloc];
    }
    
    - (void)viewDidLoad
    {
    
        // initialize an instance of MPMoviePlayerController and set it to the iVar
        MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://path/to/video.whatever"]];
        // the frame is the size of the video on the view
        mp.view.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height / 2);
        self.player = mp;
        [mp release];
        [self.view addSubview:self.player.view];
        [self.player prepareToPlay];
    
        // add tap gesture recognizer to the player.view property
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didDetectDoubleTap:)];
        tapGesture.numberOfTapsRequired = 2;
        [self.player.view addGestureRecognizer:tapGesture];
        [tapGesture release];
    
        // tell the movie to play
        [self.player play];
    
        [super viewDidLoad];
    }
    
    - (void)didDetectDoubleTap:(UITapGestureRecognizer *)tap {
        // do whatever you want to do here
        NSLog(@"double tap detected");
    }
    
    @end
    

    FYI, I've checked this code out and it works.