4

我正在下载视频并将其保存在某个目录中,以便用户之后可以播放该文件。

它适用于所有情况,例如由于某些网络波动而停止下载并再次恢复。但有时文件完全下载但没有在 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];
    }
4

3 回答 3

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;
    }
}

如果它也适合你,请接受它。有一个美好的一天。

于 2012-11-27T07:26:48.830 回答
2

利用

if(moviePlayer.loadState == MPMovieLoadStatePlayable)

并检查它是否可以播放。MPMoviePlayerLoadStateDidChangeNotification如果需要,您可以使用。有关更多详细信息,请查看苹果文档。

可以这样试试

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];
}
于 2012-11-27T06:41:53.237 回答
0

对于我的场景,我最终记录了传入视频的预期内容长度,并确保在您尝试播放视频时文件大小匹配。

从这个意义上说,我可以更快地决定是否应该播放视频。如果文件大小不匹配,我只需重新开始下载。

于 2015-07-31T22:27:33.903 回答