MPMoviePlayerViewController 在 UITabBarController 中嵌入视频

发布于 2024-10-18 02:53:18 字数 2834 浏览 3 评论 0原文

我正在尝试在标签栏导航中正确实现嵌入式视频播放。在我的具体情况下,视频应以非全屏方式显示在由 UIViewController 托管、由 UITabBarController 管理的 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 方法,也不会加载或播放任何内容。

我错过了什么,为什么重新选择第二个选项卡时视频播放失败?

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

这次我不再依赖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];
}

I am trying to get an embedded video playback properly implemented within a tab-bar navigation. In my specific case, the video shall be displayed in a non-fullscreen manner on a UIView that is hosted by a UIViewController, managed by a UITabBarController.

For simplifying the example, lets say I have two tabs within my tabbar. First one shows some random stuff, second one shows the viewcontroller that hosts the embedded video.

Once the user selects the second tab, the video is loading and playing properly.

For initializing the player, I am using the following code from within my UIView derived class, triggered by the initializer (initWithFrame):

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

For starting the playback, I am using the following code, triggered by the viewWillAppear method of my UIViewController derived class:

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

If then, the user selects the first tab (while the video is still playing), I am making sure that the video is stopped as it would continue playing if that was not done:

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

Once the user selects the second tab again, the view stays blank, nothing is loaded or played even though the playVideo-method is invoked.

What am I missing, why is the video playback failing when reselecting the second tab?

==========new attempt=============

This time I stopped relying on shouldAutoplay (as suggested) but that did not make a difference.

Adapted and added code for this;

- (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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋檐 2024-10-25 02:53:18

playVideo中我认为应该是
[movieViewController_.movi​​ePlayer setContentURL:fileURL_];
[movieViewController_.movi​​ePlayer play];

我认为它第一次可以工作,因为自动播放默认为 YES

In playVideo I think it should be
[movieViewController_.moviePlayer setContentURL:fileURL_];
[movieViewController_.moviePlayer play];

I assume that it works the first time because autoplay defaults to YES

懵少女 2024-10-25 02:53:18

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

MPMoviePlayerViewController is a subclass of UIViewController. If the second tab is dedicated to displaying video why not just use an instance of it as the root view controller for the second tab?

╰つ倒转 2024-10-25 02:53:18

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

Adding [movieViewController_.moviePlayer prepareToPlay] to my playVideo method does the trick (when working with remote streams).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文