iPhone - 在3.0和4.0 OS / SDK上播放视频?

时间:2010-04-21 09:53:49

标签: iphone video mpmovieplayercontroller

从3.2 iPhone OS SDK开始,播放视频确实不同。

所以我想知道是否有办法使用兼容代码(<和> 3.2)全屏播放视频,而无需为这两种情况编写代码。

我认为我们必须写两个版本的处理视频播放的课程......

祢!

3 个答案:

答案 0 :(得分:2)

基本上我基本上建议Jeff Kelly在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 :(得分: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
    }
}

答案 2 :(得分:-1)

您可能必须使用#if /#else /#endif块并编译具有适用于特定O / S级别的正确可执行文件的通用二进制文件。