在使用MPMoviePlayerController播放视频时,需要添加自定义控件,同时需要与MPMoviePlayerController默认的播放控制面板一起隐藏与显示
播放:
_moviePlayer = [[MPMoviePlayerController alloc] init];
_moviePlayer.contentURL = sourceMovieURL;
_moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.repeatMode = MPMovieRepeatModeNone;
_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
_moviePlayer.view.frame = self.bounds;
[_moviePlayer setFullscreen:YES animated:YES];
[self addSubview: _moviePlayer.view];
if (![HelperClass CheckIsVideo:fileFullName]) {
_about = true;
} else {
[_moviePlayer play];
}
_timerCheck = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkShowControls:) userInfo:nil repeats:true];
[_timerCheck fire];
定时器控制自定义控件的显示与隐藏(通过检测 MPMoviePlayerController的view结构来控制):
<pre name="code" class="objc">// 设置自定义按钮是否显示
-(void)setControlsShow:(BOOL)show
{
if (show) {
if (!_btnReturn.superview) {
[_moviePlayer.view addSubview:_btnReturn];
}
} else {
if (_btnReturn.superview) {
[_btnReturn removeFromSuperview];
}
}
}
// 检测控制面板是否显示
-(void)checkShowControls:(NSTimer *)timer
{
BOOL showControls = false;
do{
NSArray* viewSub = _moviePlayer.view.subviews;
if (!viewSub || viewSub.count<=0 || !viewSub[0])
break;
UIView* swapview =viewSub[0];
viewSub = swapview.subviews;
if (!viewSub || viewSub.count<=0 || !viewSub[0])
break;
UIView* videoConView = viewSub[0];
viewSub =videoConView.subviews;
if (!viewSub || viewSub.count<4 || !viewSub[3])
break;
UIView* tmpview = viewSub[3];
CGRect rect = [tmpview convertRect:tmpview.frame fromView:nil];
if (CGRectIsEmpty(rect) || CGRectIsNull(rect))
break;
// ios6此处就可能检测出是否隐藏
if (tmpview.hidden)
break;
if (CGSizeEqualToSize(rect.size, CGSizeZero))
break;
// ios6以上需要此处来检测
if (viewSub.count>=5) {
tmpview = viewSub[4];
if (CGRectIsEmpty(rect) || CGRectIsNull(rect))
break;
if (tmpview.hidden)
break;
if (CGSizeEqualToSize(rect.size, CGSizeZero))
break;
}
showControls = true;
}while (0);
[self setControlsShow:showControls];
}