2012-08-01 67 views
1

在我的应用程序播放视频的视图之一的部分黑色背景,包含下面的代码iPhone:MPMoviePlayer显示第二最初

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会在开始播放视频之前显示黑屏,并显示秒数。

我不想黑屏第二。

我为此做了什么?

thanx提前。

+0

相关:http://stackoverflow.com/questions/8429701/mpmovieplayercontroller-showing-black-empty-screen – bummi 2013-10-20 12:36:00

回答

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"); 
    } 
} 
+0

@property(强,非原子)的MPMoviePlayerController *播放器;我编写这段代码,但它会导致同样的问题。 – kEvin 2012-08-01 12:03:20

+0

我编辑我的帖子,谢谢。 – 2012-08-01 12:49:46