Search code examples
iphoneobjective-ciosmpmovieplayercontrollermpmovieplayer

Can MPMoviePlayerViewController return error if video file is corrupt?


I am downloading the video and saved it in some directory so user can play that file afterwards.

It works well in all the cases like when download stopped and resume again due to some network fluctation. But sometimes file downloaded completely but not playing in MPMoviePlayerViewController.

I am using ASIHTTPRequest to download video file in background.

Observation: May be while downloading, network fluctuates some times and file may be corrupted.

Question: How can I came to know that downloaded file is corrupted? (via MPMoviePlayerViewControll)

Any suggestions? Below is the code to play:

@ACB... I used your code, but it always going in else condition:

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

Solution

  • I found the similar problem, and I resolve this issue. Try below code:

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

    Accept it if it works for you too. Hav a nice day.