MPMoviePlayerViewController,在UITabBarController中嵌入了视频

时间:2011-02-19 16:20:06

标签: iphone uitabbarcontroller mpmovieplayercontroller mpmovieplayer

我正在尝试在标签栏导航中正确实现嵌入式视频播放。在我的特定情况下,视频应以非全屏方式显示在由UITabBarController管理的UIViewController托管的UIView上。

为简化示例,假设我的标签栏中有两个标签。第一个显示一些随机的东西,第二个显示托管嵌入视频的视图控制器。

用户选择第二个标签后,视频正在加载并正常播放。

为了初始化播放器,我在UIView派生类中使用以下代码,由初始化器(initWithFrame)触发:

- (void)initPlayback
{
     self.movieViewController = [[MPMoviePlayerViewController alloc] init];
     movieViewController_.wantsFullScreenLayout = NO;
     movieViewController_.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
     [self addSubview:self.movieViewController.view];
}

为了开始播放,我使用以下代码,由我的UIViewController派生类的viewWillAppear方法触发:

- (void)playVideo
{
     [movieViewController_.moviePlayer setContentURL:fileURL_];
}

如果那时,用户选择第一个标签(视频仍在播放时),我确保视频已停止,因为如果没有播放,视频会继续播放:

- (void)stopVideo
{
     [movieViewController_.moviePlayer stop];
}

一旦用户再次选择第二个标签,即使调用了playVideo-method,视图也会保持空白,不会加载或播放任何内容。

我缺少什么,为什么重新选择第二个标签时视频播放失败?

==========新尝试=============

这次我停止依赖于shouldAutoplay(如建议的那样),但这并没有什么区别。

为此修改并添加了代码;

- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification
{
    if (movieViewController_.moviePlayer.loadState == MPMovieLoadStatePlayable &&
        movieViewController_.moviePlayer.playbackState != MPMoviePlaybackStatePlaying)
    {
        [movieViewController_.moviePlayer play];
    }
}

- (void)deregisterFromNotifications
{
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:MPMoviePlayerLoadStateDidChangeNotification 
                                                  object:nil];

}


- (void)registerForNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(MPMoviePlayerLoadStateDidChange:) 
                                                 name:MPMoviePlayerLoadStateDidChangeNotification 
                                               object:nil];
}

- (void)initPlayback
{
    NSLog(@"playback init...");
    self.movieViewController = [[MPMoviePlayerViewController alloc] init];
    movieViewController_.wantsFullScreenLayout = NO;
    movieViewController_.moviePlayer.shouldAutoplay = NO;
    movieViewController_.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
    movieViewController_.moviePlayer.currentPlaybackTime = 0.0f;
    [self addSubview:movieViewController_.view];
}

- (void)playVideo
{
    NSLog(@"playback starting...");
    [self registerForNotifications];
    [movieViewController_.moviePlayer setContentURL:fileURL_];
}

- (void)stopVideo
{
    NSLog(@"playback stopping...");
    [movieViewController_.moviePlayer stop];
    [self deregisterFromNotifications];
}

3 个答案:

答案 0 :(得分:0)

playVideo我认为应该是 [movieViewController_.moviePlayer setContentURL:fileURL_]; [movieViewController_.moviePlayer play];

我认为它第一次有效,因为自动播放默认为YES

答案 1 :(得分:0)

MPMoviePlayerViewControllerUIViewController的子类。如果第二个选项卡专用于显示视频,为什么不仅仅使用它的实例作为第二个选项卡的根视图控制器?

答案 2 :(得分:0)

将[movieViewController_.movi​​ePlayer prepareToPlay]添加到我的playVideo方法可以解决问题(使用远程流时)。