4

我正在下載視頻並將其保存在某個目錄中,以便用戶隨後可以播放該文件。如果視頻文件損壞,MPMoviePlayerViewController會返回錯誤嗎?

它適用於所有情況下,如下載停止並由於某種網絡波動而再次恢復。但有時文件完全下載,但不能在MPMoviePlayerViewController中播放。

我使用ASIHTTPRequest在後臺下載視頻文件。

觀察:可能在下載時,網絡有時會波動,文件可能會損壞。

問題:我怎麼才能知道下載的文件已損壞? (通過MPMoviePlayerViewControll)

有什麼建議嗎?下面是玩代碼:

@ACB ...我用你的代碼,但它總是在其他條件去:

playerViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:url]; 

    player = [playerViewController moviePlayer]; 
    player.movieSourceType = MPMovieSourceTypeFile; 
    [player prepareToPlay]; 

    if(player.loadState == MPMovieLoadStatePlayable) 
    { 
     playerViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
     [self presentMoviePlayerViewControllerAnimated:playerViewController]; 


     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerInterruptByUser:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:playerViewController.moviePlayer]; 

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:playerViewController.moviePlayer]; 

     //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerFinished:) name:UIApplicationDidEnterBackgroundNotification object:playerViewController.moviePlayer]; 

     [player play]; 
    } 
    else 
    { 
     corruptVideoAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Corrupt Video", nil) message:NSLocalizedString(@"This video is corrupted due to some network error. We suggest you to download again. Do you want to download it again?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil),nil]; 
     [corruptVideoAlert show]; 
     [corruptVideoAlert release]; 
    } 

回答

3

我發現類似的問題,我解決這個問題。嘗試下面的代碼:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerPlayingErrorNotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:playerViewController.moviePlayer]; 

-(void)playerPlayingErrorNotification:(NSNotification*)notif 
{ 

    NSNumber* reason = [[notif userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; 
    switch ([reason intValue]) { 
     case MPMovieFinishReasonPlaybackEnded: 
      NSLog(@"Playback Ended");   
      break; 
     case MPMovieFinishReasonPlaybackError: 
      NSLog(@"Playback Error"); 
      [self performSelector:@selector(CorruptVideoAlertView) withObject:nil afterDelay:1.0]; 
      break; 
     case MPMovieFinishReasonUserExited: 
      NSLog(@"User Exited"); 
      break; 
     default: 
      break; 
    } 
} 

接受它,如果它也適合你。祝你有美好的一天。

+0

感謝dilip。我會檢查出來的。 –

+0

它完美無瑕。謝謝迪利普 –

2

使用

if(moviePlayer.loadState == MPMovieLoadStatePlayable) 

,並檢查它是否是可玩。如果需要,您可以使用MPMoviePlayerLoadStateDidChangeNotification。有關詳細信息check apple documentation.

,您可以嘗試這樣,

playerViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:url]; 

player = [playerViewController moviePlayer]; 
player.movieSourceType = MPMovieSourceTypeFile; 
[player prepareToPlay]; 

playerViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
[self presentMoviePlayerViewControllerAnimated:playerViewController]; 


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerInterruptByUser:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:playerViewController.moviePlayer]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:playerViewController.moviePlayer]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:playerViewController.moviePlayer]; 

    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerFinished:) name:UIApplicationDidEnterBackgroundNotification object:playerViewController.moviePlayer]; 

loadStateChanged:方法做到以下幾點,

if(player.loadState == MPMovieLoadStatePlayable) 
{ 
[player play]; 
} 
else 
{ 
    corruptVideoAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Corrupt Video", nil) message:NSLocalizedString(@"This video is corrupted due to some network error. We suggest you to download again. Do you want to download it again?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil),nil]; 
    [corruptVideoAlert show]; 
    [corruptVideoAlert release]; 
} 
+0

它會真的起作用嗎?讓我嘗試!! –

+0

我在哪裏使用這個代碼? –

+0

@swatisharma,當你嘗試使用上述代碼時發生了什麼? – iDev

0

對於我的場景,我最終錄製了進入視頻的預期內容長度,並確保在嘗試播放視頻時文件大小相匹配。

從這個意義上說,我決定是否應該播放視頻的速度更快。如果文件大小不匹配,我只需重新啓動下載。