于是,解决方案有两种可能,一是在原有控件的基础上修改使其满足项目需求,二是换控件。
笔者先试试前者。iOS6下慎用MPMoviePlayerViewController!看完着篇文章后,我就试后者。换控件为MPMoviePlayerController。
MPMoviePlayerController可满足上述的项目需求,还能满足在视频上贴广告和跳转按钮。由于player的controlStyle是MPMovieControlStyleFullscreen。所以右上角有一个切换屏幕状态的按钮,两种状态分别是MPMovieScalingModeAspectFill和MPMovieScalingModeAspectFit。前文提到本项目需贴图和跳转按钮,因此需要加检测该屏幕切换状态的通知即MPMoviePlayerScalingModeDidChangeNotification。
添加通知代码如下:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerModeChange:) name:MPMoviePlayerScalingModeDidChangeNotification object:player];
通知函数实现代码:
-(void) moviePlayerModeChange:(NSNotification *)notification
{
NSLog(@"mode change\\n");
MPMoviePlayerController *player = notification.object;
if (player.scalingMode==MPMovieScalingModeAspectFill) {
NSLog(@"fill");
}
if (player.scalingMode==MPMovieScalingModeAspectFit) {
NSLog(@"fit");
}
}
我发现以上代码在MPMoviePlayerController中只会输出“mode change”并不会进入一下任一if语句。经过测试发现moviePlayerModeChange执行过,player.scalingMode一直是MPMovieScalingModeNone。我觉得这一也是ios的一个bug。此bug可以认为加一个flag标识来记录当前状态来弥补。
而以上代码在控件MPMoviePlayerViewController中是可以很好运行的。“fill”和“fit”接替输出。
由于,MPMoviePlayerViewContoller是在MPMoviePlayerController后面出现的一个控件,且前者是在后者基础上封装上一层View。笔者推测除此之外,前者对后者还在上述通知进行了二次封装。