4

ビデオをダウンロードしてディレクトリに保存し、ユーザーが後でそのファイルを再生できるようにします。

これは、ネットワークの変動によりダウンロードが停止して再開した場合など、すべての場合にうまく機能します。ただし、ファイルが完全にダウンロードされても、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];
    }
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 に答える