开发者

How to subclass MPMoviePlayer controller

I am not sure if 开发者_Python百科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


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.


If you're not sure what subclassing means, you should research the topic "inheritance". There should be a lot of material covering the subject for iOS specifically, when you create any file in Xcode, most likely you're already subclassing a class. Most of the time you'll be subclassing NSObject or UIViewController when creating a view etc.

You probably don't want to subclass MPMoviePlayerController as it's quite an advanced class built for streaming. MPMoviePlayerViewController works like a normal view controller only it already comes with a MPMoviePlayerController as an iVar.

condensed declaration below:

@interface MPMoviePlayerViewController : UIViewController {}
@property(nonatomic, readonly) MPMoviePlayerController *moviePlayer;
@end

You can find some sample code in the Apple documentation center here:

https://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingVideo/UsingVideo.html#//apple_ref/doc/uid/TP40009767-CH3-SW1

Movie player Xcode project you'll find here:

https://developer.apple.com/library/ios/#samplecode/MoviePlayer_iPhone/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007798

Playing movies from an iOS device is really easy, make sure you read it all up on Apple's documentation. Checking out the header files for those classes will also give you more of an insight into what you're dealing with.

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜