如何子类MPMoviePlayer控制器

时间:2011-07-09 09:43:25

标签: iphone objective-c mpmovieplayercontroller mpmovieplayer

我不确定我是否理解'MoviePlayerController'的子类意味着什么。 a)这是在创建新视图控制器并在其中添加MPMoviePlayerController实例时吗? b)还有别的吗?一些代码示例非常有用。

由于

2 个答案:

答案 0 :(得分:3)

这可能不是上面的评论,因为它占用太多字符。

好的@ 1110我假设您想要在玩家视图中添加UITapGestureRecognizer,不要忘记它已经支持全屏幕/全屏移除的捏合手势。 下面的代码假设您使用MPMoviePlayerController作为iVar的视图控制器。

您可能不想检测单击,因为它已经使用点击检测计数1来显示/隐藏玩家控制器。

下面是一个代码示例,您可以使用双击手势识别器

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

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

仅供参考,我已经检查了这段代码并且有效。

答案 1 :(得分:2)

如果你不确定子类的含义,你应该研究“继承”这个主题。应该有很多关于iOS主题的材料,当你在Xcode中创建任何文件时,很可能你已经在继承了一个类。大多数情况下,在创建视图等时,您将继承NSObject或UIViewController。

您可能不希望将MPMoviePlayerController子类化,因为它是为流式传输而构建的高级类。 MPMoviePlayerViewController的工作方式与普通视图控制器类似,只是它已经将MPMoviePlayerController作为iVar附带。

以下简明声明:

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

您可以在Apple文档中心找到一些示例代码:

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

您可以在这里找到的电影播放器​​Xcode项目:

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

从iOS设备播放电影非常简单,请务必在Apple的文档中全部阅读。检查这些类的头文件还可以让您更深入地了解您正在处理的内容。

希望有所帮助。

相关问题