views:

182

answers:

1

Hello,

I'm writing an app that will be playing videos alot using the MPMoviePlayerController on the iPad. The problem is the app was working fine and playing the videos when I stopped working about 15 hours ago but now the videos do not play. The MPMoviePlayerController will show the first frame from the video, and in full screen view I can scrub through the movie fine but when I hit play it just pauses right away. I have the code below, when debugging i noticed that when I call play it sends an MPMoviePlayerPlaybackStateDidChangeNotification with the playbackState being MPMoviePlaybackStatePlaying and then right away it sends another MPMoviePlayerPlaybackStateDidChangeNotification notification with playbackState being MPMoviePlaybackStatePaused. Not sure if that helps but please let me know if you see anything wrong in my code or have an ideas, thanks.

- (void)handleNotification:(NSNotification *)notification {
    if ([[notification name] isEqualToString:MPMoviePlayerPlaybackStateDidChangeNotification]) {
        if (_videoPlayer.playbackState == MPMoviePlaybackStatePlaying) {
            _playButtonLarge.hidden = YES;
            _scrubber.maximumValue = _videoPlayer.duration;
            [_playPauseButton setBackgroundImage:[UIImage imageNamed:@"video_controls_pause.png"] forState:UIControlStateNormal];
            if (_updateScrubberTimer == nil) {
                _updateScrubberTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateScrubber) userInfo:nil repeats:YES];
            }
        } else if (_videoPlayer.playbackState == MPMoviePlaybackStatePaused || _videoPlayer.playbackState == MPMoviePlaybackStateStopped) {
            [_playPauseButton setBackgroundImage:[UIImage imageNamed:@"video_controls_play.png"] forState:UIControlStateNormal];
            _playButtonLarge.hidden = NO;
            if (_updateScrubberTimer != nil) {
                [_updateScrubberTimer invalidate];
                _updateScrubberTimer = nil;
            }
            if (_videoPlayer.playbackState == MPMoviePlaybackStateStopped) {
                _scrubber.value = 0.0f;
                _timePlayedLabel.text = @"0:00";
                _timeRemainingLabel.text = @"-0:00";
                _videoPlayerBG.hidden = NO;
            }
        }
    } else if ([[notification name] isEqualToString:MPMoviePlayerPlaybackDidFinishNotification]) {
        NSNumber *reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
        if ([reason intValue] == MPMovieFinishReasonPlaybackEnded) {
            _videoPlayerBG.hidden = NO;
        }
        _scrubber.value = _scrubber.maximumValue;
    }
}

- (void)playPause {
    if ([_videos count] > 0) {
        if (_videoPlayer.playbackState == MPMoviePlaybackStatePaused || _videoPlayer.playbackState == MPMoviePlaybackStateStopped) {
            _playButtonLarge.hidden = YES;
            _videoPlayerBG.hidden = YES;
            if ([_videoPlayer contentURL] == nil) {
                Video *video = [_videos objectAtIndex:0];
                [_videoPlayer setContentURL:video.videoURL];
            }
            if (![_videoPlayer isPreparedToPlay]) {
                [_videoPlayer prepareToPlay];
            }
            [_videoPlayer play];
        } else if (_videoPlayer.playbackState == MPMoviePlaybackStatePlaying) {
            _playButtonLarge.hidden = NO;
            [_videoPlayer pause];
        }
    }
}
A: 

I think I figured it out, I put the following code before every call to play

if (![_videoPlayer isPreparedToPlay]) { [_videoPlayer prepareToPlay]; }

works now, if anyone else has any input let me know

marchinram