【iOS开发】解决“点击MPMoviePlayerController播放视频时,出现一个黑屏闪屏问题”
当点击某按钮播放视频时,如果在按钮触发函数时alloc MPMoviePlayerController对象,同时加载资源文件,因为加载资源文件也需要时间,这时会有一个短暂的黑色闪屏,这点体验很不好,有一个好的解决方案是,提前alloc预加载好该MoviePlayer对象,点击按钮时直接调用play。
预加载相关代码:
//预加载视频,避免出现黑屏 NSString *videoPath=[[NSBundle mainBundle]pathForResource:@"startVideo" ofType:@"mp4"]; self.movieController=[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:videoPath]]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(movieFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.movieController]; self.movieController.scalingMode=MPMovieScalingModeAspectFill; [self.view insertSubview:movieController.view belowSubview:bgImgView]; self.movieController.view.frame=CGRectMake(0,0, 1024, 768); [self.movieController setControlStyle:MPMovieControlStyleNone]; //去掉进度条 self.movieController.initialPlaybackTime = -1.0; [self.movieController setShouldAutoplay:NO]; [self.movieController prepareToPlay];
尤其注意:
[self.movieController setShouldAutoplay:NO];
[self.movieController prepareToPlay];
设置AutoPlay为NO,默认为YES,会自动播放的。郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。