2012-08-01 29 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