iPhone:MPMoviePlayer最初显示的是黑色背景

时间:2012-08-01 11:26:29

标签: iphone ios5 mpmovieplayercontroller

在我的应用程序中,我在其中一个包含以下代码的视图上播放视频

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"bgVideo" ofType:@"mov"]];
        player = [[MPMoviePlayerController alloc] initWithContentURL:url];

        [player setControlStyle:MPMovieControlStyleNone];
        player.view.frame = CGRectMake(35, 190, 245, 156);
        [self.view addSubview:player.view];
        [player play];
        [player.view setBackgroundColor:[UIColor clearColor]];

我在 viewWillAppear 方法

上编写了此代码

但是当我最初看到这个视图时,我的MPMoviePlayerController在开始播放视频之前显示黑屏。

我不想要第二个黑屏。

我为此做了什么?

提前。

1 个答案:

答案 0 :(得分:0)

也许播放器强大的属性并合成它。它奏效了。

也适用于您的情况。

我的经验,不再有问题了。如果你不能工作,请回复。


<强> Edit

我有点误会。

在开始之前显示黑屏,因为未加载视频。

您无法预测加载视频所需的时间。即使您将播放器分配给viewWillApper方法。将出现黑屏。 这个黑屏无法控制它。 YouTube应用或默认视频应用也相同。 在黑屏期间,ActivityIndi​​cator或“正在加载”消息应显示给用户。这是合理的。 最后,一般来说,视频大小和质量越大,加载时间越长。 如果你想要准确加载时间,你应该得到通知。

参考示例代码。

- (void)viewWillAppear:(BOOL)animated
{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"]];
    player = [[MPMoviePlayerController alloc] initWithContentURL:url];

    [player setControlStyle:MPMovieControlStyleNone];
    player.view.frame = CGRectMake(35, 190, 245, 156);
    [self.view addSubview:player.view];
    [player.view setBackgroundColor:[UIColor clearColor]];
    player.shouldAutoplay = NO;
    [player prepareToPlay];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadMoviePlayerStateChanged:)
                                                 name:MPMoviePlayerLoadStateDidChangeNotification
                                               object:self.player];

    [super viewWillAppear:animated];
}

- (void)loadMoviePlayerStateChanged:(NSNotification *)noti
{
    int loadState = self.player.loadState;
    if(loadState & MPMovieLoadStateUnknown)
    {
        NSLog(@"MPMovieLoadStateUnknown");
        return;
    }
    else if(loadState & MPMovieLoadStatePlayable)
    {
        NSLog(@"MPMovieLoadStatePlayable");
        [player play];
    }
    else if(loadState & MPMovieLoadStatePlaythroughOK)
    {
        NSLog(@"MPMovieLoadStatePlaythroughOK");
    } else if(loadState & MPMovieLoadStateStalled)
    {
        NSLog(@"MPMovieLoadStateStalled");
    }
}