MP 电影播放器​​视图控制器滞后于 ios 中的视频播放

Posted

技术标签:

【中文标题】MP 电影播放器​​视图控制器滞后于 ios 中的视频播放【英文标题】:MPMovie player ViewController lag the video play in ios 【发布时间】:2013-06-13 11:01:26 【问题描述】:

我想在当前视图控制器上全屏播放视频。 所以我正在使用 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];
   
   

【问题讨论】:

【参考方案1】:

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

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

removeObserver 应该是 self 而不是 playerVC

但是,这不会引起您提到的任何问题。

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

【讨论】:

以上是关于MP 电影播放器​​视图控制器滞后于 ios 中的视频播放的主要内容,如果未能解决你的问题,请参考以下文章

.mkv 视频在纹理视图中滞后

UIView 显示顺序问题?

iOS 视频播放性能模拟器与设备

MP 电影播放器​​ 视频在后台播放

iOS h5点播播放mp4视频遇到的坑,ios的h5不能播放视频等

MPMoviePlayer 控制样式条框高度