1

我在使用iPad上的MPMoviePlayerController正在編寫一個將播放視頻的應用程序。問題是應用程序工作正常,並在大約15小時前停止工作時播放視頻,但現在視頻無法播放。 MPMoviePlayerController將顯示視頻中的第一幀,並且在全屏視圖中,我可以很好地掃描電影,但是當我點擊播放時,它只是立即停頓。我有下面的代碼,當調試時,我注意到當我打電話時,它發送一個MPMoviePlayerPlaybackStateDidChangeNotification,其播放狀態是MPMoviePlaybackStatePlaying,然後立即發送另一個MPMoviePlayerPlaybackStateDidChangeNotification通知,播放狀態是MPMoviePlaybackStatePaused。不知道這是否有幫助,但請讓我知道,如果你看到我的代碼中有任何錯誤或有想法,謝謝。MPMoviePlayerController在iPad上播放錯誤

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

回答

1

我想我想通了,我把下面的代碼每次調用之前玩到現在

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

作品,如果任何人有任何輸入讓我知道