当点击某按钮播放视频时,如果在按钮触发函数时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,会自动播放的。【iOS开发】解决“点击MPMoviePlayerController播放视频时,出现一个黑屏闪屏问题”
原文地址:http://blog.csdn.net/andypan1314/article/details/43602113