如果视频文件损坏,MPMoviePlayerViewController 会返回错误吗?
Posted
技术标签:
【中文标题】如果视频文件损坏,MPMoviePlayerViewController 会返回错误吗?【英文标题】:Can MPMoviePlayerViewController return error if video file is corrupt? 【发布时间】:2012-11-27 06:38:07 【问题描述】:我正在下载视频并将其保存在某个目录中,以便用户之后可以播放该文件。
它适用于所有情况,例如由于某些网络波动而停止下载并再次恢复。但有时文件完全下载但没有在 MPMoviePlayerViewController 中播放。
我正在使用 ASIHTTPRequest 在后台下载视频文件。
观察:可能是在下载过程中,网络不稳定,文件可能损坏。
问题:我如何知道下载的文件已损坏? (通过 MPMoviePlayerViewControll)
有什么建议吗?下面是玩的代码:
@ACB...我使用了你的代码,但它总是处于 else 状态:
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];
【问题讨论】:
【参考方案1】:我发现了类似的问题,我解决了这个问题。试试下面的代码:
[[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;
如果它也适合您,请接受它。祝你有美好的一天。
【讨论】:
谢谢迪利普。我去看看。【参考方案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];
【讨论】:
@swatisharma,当您尝试上述代码时会发生什么? 它打开播放器然后关闭。 我用你的方法检查了。检查我的问题的编辑代码。 @swatisharma,更新了答案。可能您正在执行它而无需等待loadState
使用 MPMoviePlayerLoadStateDidChangeNotification
更改。【参考方案3】:
对于我的场景,我最终记录了传入视频的预期内容长度,并确保在您尝试播放视频时文件大小匹配。
从这个意义上说,我可以更快地决定是否应该播放视频。如果文件大小不匹配,我就重新开始下载。
【讨论】:
以上是关于如果视频文件损坏,MPMoviePlayerViewController 会返回错误吗?的主要内容,如果未能解决你的问题,请参考以下文章