1

我正在编写一个应用程序,它将使用 iPad 上的 MPMoviePlayerController 大量播放视频。问题是当我大约 15 小时前停止工作时,该应用程序运行良好并播放视频,但现在视频无法播放。MPMoviePlayerController 将显示视频的第一帧,在全屏视图中我可以很好地浏览电影,但是当我点击播放时它会立即暂停。我有下面的代码,在调试时我注意到当我调用 play 时,它会发送一个 MPMoviePlayerPlaybackStateDidChangeNotification,playbackState 为 MPMoviePlaybackStatePlaying,然后它立即发送另一个 MPMoviePlayerPlaybackStateDidChangeNotification 通知,playbackState 为 MPMoviePlaybackStatePaused。不确定这是否有帮助,但如果您在我的代码中发现任何错误或有想法,请告诉我,

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

1 回答 1

1

我想我想通了,我在每次调用播放之前都放了以下代码

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

现在可以使用,如果其他人有任何意见,请告诉我

于 2010-06-29T08:36:09.433 回答