我正在下载视频并将其保存在某个目录中,以便用户随后可以播放该文件。
它适用于所有情况,例如下载停止并由于某些网络波动而再次恢复。但有时文件已完全下载但未在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];
}
答案 0 :(得分: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;
}
}
如果它也适合你,请接受它。好的一天。
答案 1 :(得分: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];
}
答案 2 :(得分:0)
对于我的情况,我最终录制了视频的预期内容长度,并确保在您尝试播放视频时文件大小匹配。
从这个意义上说,我决定是否应该播放视频更快。如果文件大小不匹配,我只需重新开始下载。