0

我不確定我是否理解'MoviePlayerController'的子類是什麼意思。 a)這是在創建新的視圖控制器並在其中添加MPMoviePlayerController實例嗎? b)其他的東西?一些代碼示例會非常有幫助。如何繼承MPMoviePlayer控制器

感謝

回答

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 

僅供參考,我已經檢查了這個代碼,它的工作原理。

+0

謝謝我做了類似的東西,但它的工作:)'與視圖控制器與MPMoviePlayerController作爲一個iVar',這是我的問題的真正答案,因爲我不明白什麼時候有人說'subclass mpmovieplayer'它的真正含義,繼承或查看與iVar。謝謝。 – 1110

2

如果你不知道什麼子類的手段,你應該研究的題目是「繼承」。應該有很多材料覆蓋iOS的主題,特別是當您在Xcode中創建任何文件時,很可能您已經對一個類進行了子類化。大多數情況下,您在創建視圖時將繼承NSObject或UIViewController等。

您可能不希望將MPMoviePlayerController子類化,因爲它是一個非常適合流式傳輸的高級類。 MPMoviePlayerViewController像普通的視圖控制器一樣工作,只是它已經帶有一個MPMoviePlayerController作爲iVar。

凝結以下聲明:

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

你可以在蘋果的文檔中心這裏的一些示例代碼:

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的文檔。檢出這些類的頭文件也會讓你更深入地瞭解你正在處理的內容。

希望有所幫助。

+0

我試圖使用MPMoviePlayerViewController和一切工作正常,除了我需要檢測點擊和雙擊與自定義方法處理程序的球員。所以基本上,如果我使用「MPMoviePlayerController」實例在其子類中創建UIViewController類,對吧? – 1110

+0

+ +1努力解決一個相當愚蠢的問題。 – Till

+0

@till謝謝:) – anthonyherron