本人水平较低,还需进行优化,有不足的地方还请评论指出,多多指教,谢谢。
这里是demo地址
最初使用的是36 氪开源项目KRVideoPlayer完成视频播放功能,但是无奈全屏的时候状态栏还是竖屏,最初为了开发进度就横屏隐藏状态栏了,但是感觉后期还需要完善,so,根据KRVideoPlayer的逻辑自己进行了一个简单的优化封装(部分代码仿照KRVideoPlayer)。
将播放的视图aView添加到HFVideoPlayerControlView上即可,用法如下:
HFVideoPlayerControlView *videoPlay = [[HFVideoPlayerControlView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.width*9/16)];
videoPlay.delegate = self;
videoPlay.currentTimeLabel.text = @"12:59:59";
videoPlay.totalTimeLabel.text = @"12:59:59";
[self.view addSubview:videoPlay];
UIImageView *aView = [[UIImageView alloc] init];
aView.image = [UIImage imageNamed:@"16091G34I9-6.jpg"];
aView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
aView.frame = CGRectMake(CGRectGetMinX(videoPlay.bounds), CGRectGetMinY(videoPlay.bounds), CGRectGetWidth(videoPlay.bounds), CGRectGetHeight(videoPlay.bounds));
[videoPlay addSubview:aView];
// 这两行代码使为了让aView随着videoPlay的变化而变化
aView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
aView.contentMode = UIViewContentModeScaleAspectFit;
// 让播放视频视图处于控制视图下方
[videoPlay sendSubviewToBack:aView];
需要实现的代理:
- (void)videoPlayerControlDidCloseClick:(HFVideoPlayerControlView *)videoPlayerController {
NSLog(@"关闭");
}
- (void)videoPlayerControlDidPlayClick:(HFVideoPlayerControlView *)videoPlayerController {
NSLog(@"播放");
}
- (void)videoPlayerControlDidPauseClick:(HFVideoPlayerControlView *)videoPlayerController {
NSLog(@"暂停");
}
- (void)videoPlayerControlDidEnterFullScreenClick:(HFVideoPlayerControlView *)videoPlayerController {
NSLog(@"进入全屏");
}
- (void)videoPlayerControlDidShrinkFullScreenClick:(HFVideoPlayerControlView *)videoPlayerController {
NSLog(@"取消全屏");
}
- (void)videoPlayerControlDidChangeProgressValue:(HFVideoPlayerControlView *)videoPlayerController {
NSLog(@"改变进度");
}
注意事项:
状态栏旋转的方法使用的是[[UIApplication sharedApplication] setStatusBarOrientation:(UIInterfaceOrientationLandscapeRight) animated:YES];
以下两个注意事项都是为了让状态栏能够旋转方向而设置。
①:需在info.plist
中配置View controller-based status bar appearance
的属性设置为NO
②:需在播放视频的控制器中重写shouldAutorotate
方法,并返回NO
:
- (BOOL)shouldAutorotate {
return NO;
}
更新:
解决当前播放器的控制器是受导航控制器UINavigationController
控制的时候状态栏无法旋转的问题。
增加两个对外接口方法- (void)showInWindow;
和- (void)showInView:(UIView *)view;
分别在进入全屏和取消全屏的时候调用,进行切换全屏和小屏的切换。
然后,导航控制器UINavigationController
添加一个分类,实现下面三个方法:
#import "UINavigationController+Orientation.h"
@implementation UINavigationController (Orientation)
- (BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end
网友评论