1

我想在当前视图控制器上全屏播放视频。所以我正在使用 MPMoviePlayerViewController 播放全屏视频,但问题是它播放不流畅。播放视频时有点滞后。这是我用来播放全屏视频的代码,它存储在我的电话/ipad 文档目录中

-(void)PlayVideo:(NSString *)videoFilePath
  {
NSURL *videoURL = [NSURL fileURLWithPath:videoFilePath];

NSLog(@"videoURL: %@", videoURL);

MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];

// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:playerVC
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:playerVC.moviePlayer];

// Register this class as an observer instead
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(movieFinishedCallback:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:playerVC.moviePlayer];

// Set the modal transition style of your choice
playerVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

// Present the movie player view controller
[self presentViewController:playerVC animated:NO completion:nil];

// Start playback
[playerVC.moviePlayer prepareToPlay];
[playerVC.moviePlayer play];

  }
- (void)movieFinishedCallback:(NSNotification*)aNotification
 {
// Obtain the reason why the movie playback finished
NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

// Dismiss the view controller ONLY when the reason is not "playback ended"
if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded)
{
    MPMoviePlayerController *moviePlayer = [aNotification object];

    // Remove this class from the observers
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayer];

    // Dismiss the view controller
    [self dismissViewControllerAnimated:YES completion:Nil];
   }
   }
4

1 回答 1

0

没有发现代码有任何问题,除了

[[NSNotificationCenter defaultCenter] removeObserver:playerVC
                                            name:MPMoviePlayerPlaybackDidFinishNotification
                                          object:playerVC.moviePlayer];

removeObserver 应该是self而不是playerVC

但是,你不会像你提到的那样引起任何问题。

我建议您尝试分析您的应用程序,这可能会将您引导至导致问题的代码。播放视频时,您可能正在后台执行某些操作,导致视频延迟。

于 2013-06-13T11:18:52.987 回答