2010-04-21 34 views
5

由於3.2 iPhone OS SDK,播放視頻確實不同。iPhone - 在3.0和4.0 OS/SDK上播放視頻?

所以我想知道是否有一種方法可以使用兼容代碼(包括<和> 3.2)全屏播放視頻,而無需爲兩種情況編寫代碼。

我認爲我們將不得不寫2個版本我們班的處理視頻播放......

你的!

回答

2

我基本上做什麼上面傑夫·凱利提出對3.1運行及以上,請注意instancesRespondToSelector電話:後來在

// Initialize a movie player object with the specified URL 
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
if (mp) 
{ 

    // Register to receive a notification when the movie has finished playing. 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(moviePlayBackDidFinish:) 
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:nil]; 


    //Will only run this code for >= OS 3.2 
    if ([MPMoviePlayerController instancesRespondToSelector:@selector(setFullscreen:animated:)]){ 

     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(moviePlayBackStateDidChange:) 
                name:MPMoviePlayerPlaybackStateDidChangeNotification 
                object:nil]; 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(nowPlayingMovieDidChange:) 
                name:MPMoviePlayerNowPlayingMovieDidChangeNotification 
                object:nil]; 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(moviePlayBackDidFinish:) 
                name:MPMoviePlayerDidExitFullscreenNotification 
                object:nil]; 

     mp.controlStyle = MPMovieControlStyleFullscreen; 


     [mp setScalingMode:MPMovieScalingModeAspectFit]; 

        //change mainMenu here to whatever your parent view is 
     [mp.view setFrame:mainMenu.frame]; 
     [self.view addSubview:mp.view]; 



     [mp setFullscreen:YES animated:NO]; 
    } 
//continue as normal 

,然後moviePlayBackDidFinish函數我使用相同的技術來刪除通知。

-1

您可能必須使用#if /#else /#endif塊並編譯具有適用於特定操作系統級別的正確可執行文件的通用二進制文件。

+2

使用像這樣的預處理器宏會產生無論Active SDK設置爲什麼的效果。當你編譯應用程序時,如果你的目標是3.1.3和更高版本的iPhone SDK,它將使用3.1.3設置編譯應用程序,並且相同的二進制文件將用於兩個操作系統版本。一般來說,運行時自省(例如'if([SomeClass respondsToSelector:@selector(someSelector)])')是首選。 – 2010-04-21 15:33:49

0

一種可能性是爲此有一個輔助方法。這樣你只需要編寫一次,並在任何地方都有這個功能。

要編寫助手方法本身,您需要檢查MPMoviePlayerViewController是否可用。如果是這樣,那麼使用它,然後呈現全屏。否則,只需使用常規MPMoviePlayerController。

所以基本框架是:

-(void)playMovie:(NSURL *)movieURL 
{ 
    Class mpVC = NCClassFromString("MPMoviePlayerViewController"); 
    if(mpVC) 
    { 
     // Generate MPPlayerViewController here and use accordingly 
    } 
    else 
    { 
     // Generate MPPlayerController here and use accordingly 
    } 
}