0

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

谢谢

4

2 回答 2

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

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

于 2011-07-11T15:47:45.907 回答
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 的文档。检查这些类的头文件还可以让您更深入地了解您正在处理的内容。

希望有帮助。

于 2011-07-09T19:50:08.110 回答