Closed ljssafe closed 4 years ago
@weakify(self);
self.player.currentPlayerManager.playerPlayTimeChanged = ^(id
if ([self.player.controlView respondsToSelector:@selector(videoPlayer:currentTime:totalTime:)]) {
[self.player.controlView videoPlayer:self.player currentTime:currentTime totalTime:itemsets.duration];
}
};
重新实现了该回调,建议可以把这个丢个属性出来方便 这种需求,毕竟这种情况应该还是蛮常见的
@renzifeng 我已经改好文件了 新增了个videoTotalTime 不影响原有功能 文件要传你更新一把不 🙃 #
// // ZFPlayerCustomControlView.m // Wisburg // // Created by Brandy on 2019/11/15. // Copyright © 2019 brandy. All rights reserved. //
@interface ZFPlayerCustomControlView ()
/// 竖屏控制层的View @property (nonatomic, strong) ZFPortraitCustomControlView portraitControlView; /// 横屏控制层的View @property (nonatomic, strong) ZFLandScapeCustomControlView landScapeControlView; /// 加载loading @property (nonatomic, strong) ZFSpeedLoadingView activity; /// 快进快退View @property (nonatomic, strong) UIView fastView; /// 快进快退进度progress @property (nonatomic, strong) ZFSliderView fastProgressView; /// 快进快退时间 @property (nonatomic, strong) UILabel fastTimeLabel; /// 快进快退ImageView @property (nonatomic, strong) UIImageView fastImageView; /// 加载失败按钮 @property (nonatomic, strong) UIButton failBtn; /// 底部播放进度 @property (nonatomic, strong) ZFSliderView bottomPgrogress; /// 封面图 @property (nonatomic, strong) UIImageView coverImageView; /// 是否显示了控制层 @property (nonatomic, assign, getter=isShowing) BOOL showing; /// 是否播放结束 @property (nonatomic, assign, getter=isPlayEnd) BOOL playeEnd;
@property (nonatomic, assign) BOOL controlViewAppeared;
@property (nonatomic, assign) NSTimeInterval sumTime;
@property (nonatomic, strong) dispatch_block_t afterBlock;
@property (nonatomic, strong) ZFSmallFloatControlView *floatControlView;
@property (nonatomic, strong) ZFVolumeBrightnessView *volumeBrightnessView;
@property (nonatomic, strong) UIImageView *bgImgView;
@property (nonatomic, strong) UIView *effectView;
@end
@implementation ZFPlayerCustomControlView
@synthesize player = _player;
(instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self addAllSubViews]; self.landScapeControlView.hidden = YES; self.floatControlView.hidden = YES; self.seekToPlay = YES; self.effectViewShow = YES; self.horizontalPanShowControlView = YES; self.autoFadeTimeInterval = 0.25; self.autoHiddenTimeInterval = 2.5; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil]; } return self; }
(void)layoutSubviews { [super layoutSubviews]; CGFloat min_x = 0; CGFloat min_y = 0; CGFloat min_w = 0; CGFloat min_h = 0; CGFloat min_view_w = self.zf_width; CGFloat min_view_h = self.zf_height;
self.portraitControlView.frame = self.bounds; self.landScapeControlView.frame = self.bounds; self.floatControlView.frame = self.bounds; self.coverImageView.frame = self.bounds; self.bgImgView.frame = self.bounds; self.effectView.frame = self.bounds;
min_w = 80; min_h = 80; self.activity.frame = CGRectMake(min_x, min_y, min_w, min_h); self.activity.zf_centerX = self.zf_centerX; self.activity.zf_centerY = self.zf_centerY + 10;
min_w = 150; min_h = 30; self.failBtn.frame = CGRectMake(min_x, min_y, min_w, min_h); self.failBtn.center = self.center;
min_w = 140; min_h = 80; self.fastView.frame = CGRectMake(min_x, min_y, min_w, min_h); self.fastView.center = self.center;
min_w = 32; min_x = (self.fastView.zf_width - min_w) / 2; min_y = 5; min_h = 32; self.fastImageView.frame = CGRectMake(min_x, min_y, min_w, min_h);
min_x = 0; min_y = self.fastImageView.zf_bottom + 2; min_w = self.fastView.zf_width; min_h = 20; self.fastTimeLabel.frame = CGRectMake(min_x, min_y, min_w, min_h);
min_x = 12; min_y = self.fastTimeLabel.zf_bottom + 5; min_w = self.fastView.zf_width - 2 * min_x; min_h = 10; self.fastProgressView.frame = CGRectMake(min_x, min_y, min_w, min_h);
min_x = 0; min_y = min_view_h - 1; min_w = min_view_w; min_h = 1; self.bottomPgrogress.frame = CGRectMake(min_x, min_y, min_w, min_h);
min_x = 0; min_y = isiPhoneX ? 54 : 30; min_w = 170; min_h = 35; self.volumeBrightnessView.frame = CGRectMake(min_x, min_y, min_w, min_h); self.volumeBrightnessView.zf_centerX = self.zf_centerX; }
(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil]; [self cancelAutoFadeOutControlView]; }
/// 添加所有子控件
(void)addAllSubViews { [self addSubview:self.portraitControlView]; [self addSubview:self.landScapeControlView]; [self addSubview:self.floatControlView]; [self addSubview:self.activity]; [self addSubview:self.failBtn]; [self addSubview:self.fastView]; [self.fastView addSubview:self.fastImageView]; [self.fastView addSubview:self.fastTimeLabel]; [self.fastView addSubview:self.fastProgressView]; [self addSubview:self.bottomPgrogress]; [self addSubview:self.volumeBrightnessView]; }
(void)autoFadeOutControlView { self.controlViewAppeared = YES; [self cancelAutoFadeOutControlView]; @weakify(self) self.afterBlock = dispatch_block_create(0, ^{ @strongify(self) [self hideControlViewWithAnimated:YES]; }); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.autoHiddenTimeInterval * NSEC_PER_SEC)), dispatch_get_main_queue(),self.afterBlock); }
/// 取消延时隐藏controlView的方法
/// 隐藏控制层
/// 显示控制层
/// 音量改变的通知
/// 重置控制层
/// 设置标题、封面、全屏模式
/// 设置标题、封面、默认占位图、全屏模式
/// 设置标题、UIImage封面、全屏模式
/// 手势筛选,返回NO不响应该手势
/// 单击手势事件
/// 双击手势事件
/// 开始滑动手势事件
/// 滑动中手势事件
/// 滑动结束手势事件
/// 捏合手势事件,这里改变了视频的填充模式
/// 准备播放
/// 播放状态改变
/// 加载状态改变
/// 播放进度改变回调
(void)videoPlayer:(ZFPlayerController *)videoPlayer currentTime:(NSTimeInterval)currentTime totalTime:(NSTimeInterval)totalTime {
if (self.videoTotalTime > 0) { [self.portraitControlView videoPlayer:videoPlayer currentTime:currentTime totalTime:self.videoTotalTime]; [self.landScapeControlView videoPlayer:videoPlayer currentTime:currentTime totalTime:self.videoTotalTime]; self.bottomPgrogress.value = currentTime/self.videoTotalTime; }else { [self.portraitControlView videoPlayer:videoPlayer currentTime:currentTime totalTime:totalTime]; [self.landScapeControlView videoPlayer:videoPlayer currentTime:currentTime totalTime:totalTime]; self.bottomPgrogress.value = videoPlayer.progress; } }
/// 缓冲改变回调
(void)videoPlayer:(ZFPlayerController *)videoPlayer bufferTime:(NSTimeInterval)bufferTime {
[self.portraitControlView videoPlayer:videoPlayer bufferTime:bufferTime]; [self.landScapeControlView videoPlayer:videoPlayer bufferTime:bufferTime];
if (self.videoTotalTime > 0) { self.bottomPgrogress.bufferValue = bufferTime/self.videoTotalTime; }else { self.bottomPgrogress.bufferValue = videoPlayer.bufferProgress; } }
(void)videoPlayer:(ZFPlayerController *)videoPlayer presentationSizeChanged:(CGSize)size { [self.landScapeControlView videoPlayer:videoPlayer presentationSizeChanged:size]; }
/// 视频view即将旋转
(void)videoPlayer:(ZFPlayerController )videoPlayer orientationWillChange:(ZFOrientationObserver )observer { self.portraitControlView.hidden = observer.isFullScreen; self.landScapeControlView.hidden = !observer.isFullScreen; if (videoPlayer.isSmallFloatViewShow) { self.floatControlView.hidden = observer.isFullScreen; self.portraitControlView.hidden = YES; if (observer.isFullScreen) { self.controlViewAppeared = NO; [self cancelAutoFadeOutControlView]; } } if (self.controlViewAppeared) { [self showControlViewWithAnimated:NO]; } else { [self hideControlViewWithAnimated:NO]; }
if (observer.isFullScreen) { [self.volumeBrightnessView removeSystemVolumeView]; } else { [self.volumeBrightnessView addSystemVolumeView]; } }
/// 视频view已经旋转
/// 锁定旋转方向
/// 列表滑动时视频view已经显示
/// 列表滑动时视频view已经消失
(void)playerDidDisappearInScrollView:(ZFPlayerController *)videoPlayer { if (!self.player.stopWhileNotVisible && !videoPlayer.isFullScreen) { self.floatControlView.hidden = NO; self.portraitControlView.hidden = YES; } }
(void)videoPlayer:(ZFPlayerController *)videoPlayer floatViewShow:(BOOL)show { self.floatControlView.hidden = !show; self.portraitControlView.hidden = show; }
(void)sliderValueChangingValue:(CGFloat)value isForward:(BOOL)forward { if (self.horizontalPanShowControlView) { /// 显示控制层 [self showControlViewWithAnimated:NO]; [self cancelAutoFadeOutControlView]; }
self.fastProgressView.value = value; self.fastView.hidden = NO; self.fastView.alpha = 1; if (forward) { self.fastImageView.image = ZFPlayer_Image(@"ZFPlayer_fast_forward"); } else { self.fastImageView.image = ZFPlayer_Image(@"ZFPlayer_fast_backward"); } NSString draggedTime ; NSString totalTime ; if (self.videoTotalTime > 0) { draggedTime = [ZFUtilities convertTimeSecond:self.videoTotalTimevalue]; totalTime = [ZFUtilities convertTimeSecond:self.videoTotalTime]; }else { draggedTime = [ZFUtilities convertTimeSecond:self.player.totalTimevalue]; totalTime = [ZFUtilities convertTimeSecond:self.player.totalTime]; } self.fastTimeLabel.text = [NSString stringWithFormat:@"%@ / %@",draggedTime,totalTime]; /// 更新滑杆 [self.portraitControlView sliderValueChanged:value currentTimeString:draggedTime]; [self.landScapeControlView sliderValueChanged:value currentTimeString:draggedTime];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideFastView) object:nil]; [self performSelector:@selector(hideFastView) withObject:nil afterDelay:0.1];
if (self.fastViewAnimated) { [UIView animateWithDuration:0.4 animations:^{ self.fastView.transform = CGAffineTransformMakeTranslation(forward?8:-8, 0); }]; } }
/// 隐藏快进视图
/// 加载失败
(void)setVideoTotalTime:(NSTimeInterval)videoTotalTime { _videoTotalTime = videoTotalTime; self.portraitControlView.videoTotalTime = videoTotalTime; self.landScapeControlView.videoTotalTime = videoTotalTime; }
(void)setPlayer:(ZFPlayerController *)player { _player = player; self.landScapeControlView.player = player; self.portraitControlView.player = player; /// 解决播放时候黑屏闪一下问题 [player.currentPlayerManager.view insertSubview:self.bgImgView atIndex:0]; [self.bgImgView addSubview:self.effectView]; [player.currentPlayerManager.view insertSubview:self.coverImageView atIndex:1]; self.coverImageView.frame = player.currentPlayerManager.view.bounds; self.coverImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.bgImgView.frame = player.currentPlayerManager.view.bounds; self.bgImgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.effectView.frame = self.bgImgView.bounds; self.coverImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; }
(void)setSeekToPlay:(BOOL)seekToPlay { _seekToPlay = seekToPlay; self.portraitControlView.seekToPlay = seekToPlay; self.landScapeControlView.seekToPlay = seekToPlay; }
(void)setEffectViewShow:(BOOL)effectViewShow { _effectViewShow = effectViewShow; if (effectViewShow) { self.bgImgView.hidden = NO; } else { self.bgImgView.hidden = YES; } }
(UIImageView *)bgImgView { if (!_bgImgView) { _bgImgView = [[UIImageView alloc] init]; _bgImgView.userInteractionEnabled = YES; } return _bgImgView; }
(UIView )effectView { if (!_effectView) { if (@available(iOS 8.0, )) { UIBlurEffect effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; _effectView = [[UIVisualEffectView alloc] initWithEffect:effect]; } else { UIToolbar effectView = [[UIToolbar alloc] init]; effectView.barStyle = UIBarStyleBlackTranslucent; _effectView = effectView; } } return _effectView; }
(ZFPortraitCustomControlView *)portraitControlView { if (!_portraitControlView) { @weakify(self) _portraitControlView = [[ZFPortraitCustomControlView alloc] init]; _portraitControlView.sliderValueChanging = ^(CGFloat value, BOOL forward) { @strongify(self) [self cancelAutoFadeOutControlView]; }; _portraitControlView.sliderValueChanged = ^(CGFloat value) { @strongify(self) [self autoFadeOutControlView]; }; } return _portraitControlView; }
(ZFLandScapeCustomControlView *)landScapeControlView { if (!_landScapeControlView) { @weakify(self) _landScapeControlView = [[ZFLandScapeCustomControlView alloc] init]; _landScapeControlView.sliderValueChanging = ^(CGFloat value, BOOL forward) { @strongify(self) [self cancelAutoFadeOutControlView]; }; _landScapeControlView.sliderValueChanged = ^(CGFloat value) { @strongify(self) [self autoFadeOutControlView]; }; } return _landScapeControlView; }
(ZFSpeedLoadingView *)activity { if (!_activity) { _activity = [[ZFSpeedLoadingView alloc] init]; } return _activity; }
(UIView *)fastView { if (!_fastView) { _fastView = [[UIView alloc] init]; _fastView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7]; _fastView.layer.cornerRadius = 4; _fastView.layer.masksToBounds = YES; _fastView.hidden = YES; } return _fastView; }
(UIImageView *)fastImageView { if (!_fastImageView) { _fastImageView = [[UIImageView alloc] init]; } return _fastImageView; }
(UILabel *)fastTimeLabel { if (!_fastTimeLabel) { _fastTimeLabel = [[UILabel alloc] init]; _fastTimeLabel.textColor = [UIColor whiteColor]; _fastTimeLabel.textAlignment = NSTextAlignmentCenter; _fastTimeLabel.font = [UIFont systemFontOfSize:14.0]; _fastTimeLabel.adjustsFontSizeToFitWidth = YES; } return _fastTimeLabel; }
(ZFSliderView *)fastProgressView { if (!_fastProgressView) { _fastProgressView = [[ZFSliderView alloc] init]; _fastProgressView.maximumTrackTintColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4]; _fastProgressView.minimumTrackTintColor = [UIColor whiteColor]; _fastProgressView.sliderHeight = 2; _fastProgressView.isHideSliderBlock = NO; } return _fastProgressView; }
(UIButton *)failBtn { if (!_failBtn) { _failBtn = [UIButton buttonWithType:UIButtonTypeSystem]; [_failBtn setTitle:@"加载失败,点击重试" forState:UIControlStateNormal]; [_failBtn addTarget:self action:@selector(failBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [_failBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _failBtn.titleLabel.font = [UIFont systemFontOfSize:14.0]; _failBtn.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7]; _failBtn.hidden = YES; } return _failBtn; }
(ZFSliderView *)bottomPgrogress { if (!_bottomPgrogress) { _bottomPgrogress = [[ZFSliderView alloc] init]; _bottomPgrogress.maximumTrackTintColor = [UIColor clearColor]; _bottomPgrogress.minimumTrackTintColor = [UIColor whiteColor]; _bottomPgrogress.bufferTrackTintColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5]; _bottomPgrogress.sliderHeight = 1; _bottomPgrogress.isHideSliderBlock = NO; } return _bottomPgrogress; }
(UIImageView *)coverImageView { if (!_coverImageView) { _coverImageView = [[UIImageView alloc] init]; _coverImageView.userInteractionEnabled = YES; _coverImageView.contentMode = UIViewContentModeScaleAspectFit; } return _coverImageView; }
(ZFSmallFloatControlView *)floatControlView { if (!_floatControlView) { _floatControlView = [[ZFSmallFloatControlView alloc] init]; @weakify(self) _floatControlView.closeClickCallback = ^{ @strongify(self) if (self.player.containerType == ZFPlayerContainerTypeCell) { [self.player stopCurrentPlayingCell]; } else if (self.player.containerType == ZFPlayerContainerTypeView) { [self.player stopCurrentPlayingView]; } [self resetControlView]; }; } return _floatControlView; }
(ZFVolumeBrightnessView *)volumeBrightnessView { if (!_volumeBrightnessView) { _volumeBrightnessView = [[ZFVolumeBrightnessView alloc] init]; _volumeBrightnessView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7]; } return _volumeBrightnessView; }
(void)setBackBtnClickCallback:(void (^)(void))backBtnClickCallback { _backBtnClickCallback = [backBtnClickCallback copy]; self.landScapeControlView.backBtnClickCallback = _backBtnClickCallback; }
@end
// // ZFPortraitCustomControlView.m // Wisburg // // Created by Brandy on 2019/11/15. // Copyright © 2019 brandy. All rights reserved. //
@interface ZFPortraitCustomControlView ()
@property (nonatomic, assign) BOOL isShow;
@end
@implementation ZFPortraitCustomControlView
(instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // 添加子控件 [self addSubview:self.topToolView]; [self addSubview:self.bottomToolView]; [self addSubview:self.playOrPauseBtn]; [self.topToolView addSubview:self.titleLabel]; [self.bottomToolView addSubview:self.currentTimeLabel]; [self.bottomToolView addSubview:self.slider]; [self.bottomToolView addSubview:self.totalTimeLabel]; [self.bottomToolView addSubview:self.fullScreenBtn];
// 设置子控件的响应事件
[self makeSubViewsAction];
[self resetControlView];
self.clipsToBounds = YES;
} return self; }
(void)makeSubViewsAction { [self.playOrPauseBtn addTarget:self action:@selector(playPauseButtonClickAction:) forControlEvents:UIControlEventTouchUpInside]; [self.fullScreenBtn addTarget:self action:@selector(fullScreenButtonClickAction:) forControlEvents:UIControlEventTouchUpInside]; }
(void)sliderTouchBegan:(float)value { self.slider.isdragging = YES; }
(void)sliderTouchEnded:(float)value { if (self.player.totalTime > 0) { if (self.videoTotalTime > 0) { @weakify(self) [self.player seekToTime:self.videoTotalTime value completionHandler:^(BOOL finished) { @strongify(self) if (finished) { self.slider.isdragging = NO; } }]; }else { @weakify(self) [self.player seekToTime:self.player.totalTime value completionHandler:^(BOOL finished) { @strongify(self) if (finished) { self.slider.isdragging = NO; } }]; }
if (self.seekToPlay) {
[self.player.currentPlayerManager play];
}
} else { self.slider.isdragging = NO; } if (self.sliderValueChanged) self.sliderValueChanged(value); }
(void)sliderValueChanged:(float)value { if (self.player.totalTime == 0) { self.slider.value = 0; return; } self.slider.isdragging = YES; NSString currentTimeString = [ZFUtilities convertTimeSecond:self.player.totalTime value]; if (self.videoTotalTime > 0) { currentTimeString = [ZFUtilities convertTimeSecond:self. videoTotalTime * value]; } self.currentTimeLabel.text = currentTimeString; if (self.sliderValueChanging) self.sliderValueChanging(value,self.slider.isForward); }
(void)sliderTapped:(float)value { if (self.player.totalTime > 0) { self.slider.isdragging = YES; if (self.videoTotalTime > 0) { @weakify(self) [self.player seekToTime:self.videoTotalTime value completionHandler:^(BOOL finished) { @strongify(self) if (finished) { self.slider.isdragging = NO; [self.player.currentPlayerManager play]; } }]; }else { @weakify(self) [self.player seekToTime:self.player.totalTime value completionHandler:^(BOOL finished) { @strongify(self) if (finished) { self.slider.isdragging = NO; [self.player.currentPlayerManager play]; } }]; }
} else { self.slider.isdragging = NO; self.slider.value = 0; } }
(void)playPauseButtonClickAction:(UIButton *)sender { [self playOrPause]; }
(void)fullScreenButtonClickAction:(UIButton *)sender { [self.player enterFullScreen:YES animated:YES]; }
/// 根据当前播放状态取反
(void)playOrPause { self.playOrPauseBtn.selected = !self.playOrPauseBtn.isSelected; self.playOrPauseBtn.isSelected? [self.player.currentPlayerManager play]: [self.player.currentPlayerManager pause]; }
(void)playBtnSelectedState:(BOOL)selected { self.playOrPauseBtn.selected = selected; }
(void)layoutSubviews { [super layoutSubviews];
CGFloat min_x = 0; CGFloat min_y = 0; CGFloat min_w = 0; CGFloat min_h = 0; CGFloat min_view_w = self.bounds.size.width; CGFloat min_view_h = self.bounds.size.height; CGFloat min_margin = 9;
min_x = 0; min_y = 0; min_w = min_view_w; min_h = 40; self.topToolView.frame = CGRectMake(min_x, min_y, min_w, min_h);
min_x = 15; min_y = 5; min_w = min_view_w - min_x - 15; min_h = 30; self.titleLabel.frame = CGRectMake(min_x, min_y, min_w, min_h);
min_h = 40; min_x = 0; min_y = min_view_h - min_h; min_w = min_view_w; self.bottomToolView.frame = CGRectMake(min_x, min_y, min_w, min_h);
min_x = 0; min_y = 0; min_w = 44; min_h = min_w; self.playOrPauseBtn.frame = CGRectMake(min_x, min_y, min_w, min_h); self.playOrPauseBtn.center = self.center;
min_x = min_margin; min_w = 62; min_h = 28; min_y = (self.bottomToolView.zf_height - min_h)/2; self.currentTimeLabel.frame = CGRectMake(min_x, min_y, min_w, min_h);
min_w = 28; min_h = min_w; min_x = self.bottomToolView.zf_width - min_w - min_margin; min_y = 0; self.fullScreenBtn.frame = CGRectMake(min_x, min_y, min_w, min_h); self.fullScreenBtn.zf_centerY = self.currentTimeLabel.zf_centerY;
min_w = 62; min_h = 28; min_x = self.fullScreenBtn.zf_left - min_w - 4; min_y = 0; self.totalTimeLabel.frame = CGRectMake(min_x, min_y, min_w, min_h); self.totalTimeLabel.zf_centerY = self.currentTimeLabel.zf_centerY;
min_x = self.currentTimeLabel.zf_right + 4; min_y = 0; min_w = self.totalTimeLabel.zf_left - min_x - 4; min_h = 30; self.slider.frame = CGRectMake(min_x, min_y, min_w, min_h); self.slider.zf_centerY = self.currentTimeLabel.zf_centerY;
if (!self.isShow) { self.topToolView.zf_y = -self.topToolView.zf_height; self.bottomToolView.zf_y = self.zf_height; self.playOrPauseBtn.alpha = 0; } else { self.topToolView.zf_y = 0; self.bottomToolView.zf_y = self.zf_height - self.bottomToolView.zf_height; self.playOrPauseBtn.alpha = 1; } }
/* 重置ControlView /
(void)resetControlView { self.bottomToolView.alpha = 1; self.slider.value = 0; self.slider.bufferValue = 0; self.currentTimeLabel.text = @"00:00"; self.totalTimeLabel.text = @"00:00"; self.backgroundColor = [UIColor clearColor]; self.playOrPauseBtn.selected = YES; self.titleLabel.text = @""; }
(void)showControlView { self.topToolView.alpha = 1; self.bottomToolView.alpha = 1; self.isShow = YES; self.topToolView.zf_y = 0; self.bottomToolView.zf_y = self.zf_height - self.bottomToolView.zf_height; self.playOrPauseBtn.alpha = 1; self.player.statusBarHidden = NO; }
(void)hideControlView { self.isShow = NO; self.topToolView.zf_y = -self.topToolView.zf_height; self.bottomToolView.zf_y = self.zf_height; self.player.statusBarHidden = NO; self.playOrPauseBtn.alpha = 0; self.topToolView.alpha = 0; self.bottomToolView.alpha = 0; }
(BOOL)shouldResponseGestureWithPoint:(CGPoint)point withGestureType:(ZFPlayerGestureType)type touch:(nonnull UITouch *)touch { CGRect sliderRect = [self.bottomToolView convertRect:self.slider.frame toView:self]; if (CGRectContainsPoint(sliderRect, point)) { return NO; } return YES; }
(void)videoPlayer:(ZFPlayerController )videoPlayer currentTime:(NSTimeInterval)currentTime totalTime:(NSTimeInterval)totalTime { if (!self.slider.isdragging) { NSString currentTimeString = [ZFUtilities convertTimeSecond:currentTime]; self.currentTimeLabel.text = currentTimeString; NSString *totalTimeString = [ZFUtilities convertTimeSecond:totalTime]; self.totalTimeLabel.text = totalTimeString;
if (self.videoTotalTime > 0) {
self.slider.value = currentTime/self.videoTotalTime;
}else {
self.slider.value = videoPlayer.progress;
}
} }
(void)videoPlayer:(ZFPlayerController *)videoPlayer bufferTime:(NSTimeInterval)bufferTime { if (self.videoTotalTime > 0) { self.slider.bufferValue = bufferTime/self.videoTotalTime; }else { self.slider.bufferValue = videoPlayer.bufferProgress; } }
(void)showTitle:(NSString *)title fullScreenMode:(ZFFullScreenMode)fullScreenMode { self.titleLabel.text = title; self.player.orientationObserver.fullScreenMode = fullScreenMode; }
/// 调节播放进度slider和当前时间更新
/// 滑杆结束滑动
(UIView )topToolView { if (!_topToolView) { _topToolView = [[UIView alloc] init]; UIImage image = ZFPlayer_Image(@"ZFPlayer_top_shadow"); _topToolView.layer.contents = (id)image.CGImage; } return _topToolView; }
(UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.textColor = [UIColor whiteColor]; _titleLabel.font = [UIFont systemFontOfSize:15.0]; } return _titleLabel; }
(UIView )bottomToolView { if (!_bottomToolView) { _bottomToolView = [[UIView alloc] init]; UIImage image = ZFPlayer_Image(@"ZFPlayer_bottom_shadow"); _bottomToolView.layer.contents = (id)image.CGImage; } return _bottomToolView; }
(UIButton *)playOrPauseBtn { if (!_playOrPauseBtn) { _playOrPauseBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_playOrPauseBtn setImage:ZFPlayer_Image(@"new_allPlay44x44") forState:UIControlStateNormal]; [_playOrPauseBtn setImage:ZFPlayer_Image(@"new_allPause44x44") forState:UIControlStateSelected]; } return _playOrPauseBtn; }
(UILabel *)currentTimeLabel { if (!_currentTimeLabel) { _currentTimeLabel = [[UILabel alloc] init]; _currentTimeLabel.textColor = [UIColor whiteColor]; _currentTimeLabel.font = [UIFont systemFontOfSize:14.0f]; _currentTimeLabel.textAlignment = NSTextAlignmentCenter; } return _currentTimeLabel; }
(ZFSliderView *)slider { if (!_slider) { _slider = [[ZFSliderView alloc] init]; _slider.delegate = self; _slider.maximumTrackTintColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.8]; _slider.bufferTrackTintColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5]; _slider.minimumTrackTintColor = [UIColor whiteColor]; [_slider setThumbImage:ZFPlayer_Image(@"ZFPlayer_slider") forState:UIControlStateNormal]; _slider.sliderHeight = 2; } return _slider; }
(UILabel *)totalTimeLabel { if (!_totalTimeLabel) { _totalTimeLabel = [[UILabel alloc] init]; _totalTimeLabel.textColor = [UIColor whiteColor]; _totalTimeLabel.font = [UIFont systemFontOfSize:14.0f]; _totalTimeLabel.textAlignment = NSTextAlignmentCenter; } return _totalTimeLabel; }
(UIButton *)fullScreenBtn { if (!_fullScreenBtn) { _fullScreenBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_fullScreenBtn setImage:ZFPlayer_Image(@"ZFPlayer_fullscreen") forState:UIControlStateNormal]; } return _fullScreenBtn; }
@end
// // ZFLandScapeCustomControlView.m // Wisburg // // Created by Brandy on 2019/11/15. // Copyright © 2019 brandy. All rights reserved. //
@interface ZFLandScapeCustomControlView ()
/// 顶部工具栏 @property (nonatomic, strong) UIView topToolView; /// 返回按钮 @property (nonatomic, strong) UIButton backBtn; /// 标题 @property (nonatomic, strong) UILabel titleLabel; /// 底部工具栏 @property (nonatomic, strong) UIView bottomToolView; /// 播放或暂停按钮 @property (nonatomic, strong) UIButton playOrPauseBtn; /// 播放的当前时间 @property (nonatomic, strong) UILabel currentTimeLabel; /// 滑杆 @property (nonatomic, strong) ZFSliderView slider; /// 视频总时间 @property (nonatomic, strong) UILabel totalTimeLabel; /// 锁定屏幕按钮 @property (nonatomic, strong) UIButton *lockBtn;
@property (nonatomic, assign) BOOL isShow;
@end
@implementation ZFLandScapeCustomControlView
(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; }
(instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self addSubview:self.topToolView]; [self.topToolView addSubview:self.backBtn]; [self.topToolView addSubview:self.titleLabel]; [self addSubview:self.bottomToolView]; [self.bottomToolView addSubview:self.playOrPauseBtn]; [self.bottomToolView addSubview:self.currentTimeLabel];
[self.bottomToolView addSubview:self.slider];
[self.bottomToolView addSubview:self.totalTimeLabel];
[self addSubview:self.lockBtn];
// 设置子控件的响应事件
[self makeSubViewsAction];
[self resetControlView];
/// statusBarFrame changed
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(layOutControllerViews) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
} return self; }
(void)layoutSubviews { [super layoutSubviews]; CGFloat min_x = 0; CGFloat min_y = 0; CGFloat min_w = 0; CGFloat min_h = 0; CGFloat min_view_w = self.bounds.size.width; CGFloat min_view_h = self.bounds.size.height;
CGFloat min_margin = 9;
min_x = 0; min_y = 0; min_w = min_view_w; min_h = iPhoneX ? 110 : 80; self.topToolView.frame = CGRectMake(min_x, min_y, min_w, min_h);
min_x = (iPhoneX && self.player.orientationObserver.fullScreenMode == ZFFullScreenModeLandscape) ? 44: 15; min_y = (iPhoneX && self.player.orientationObserver.fullScreenMode == ZFFullScreenModeLandscape) ? 15: (iPhoneX ? 40 : 20); min_w = 40; min_h = 40; self.backBtn.frame = CGRectMake(min_x, min_y, min_w, min_h);
min_x = self.backBtn.zf_right + 5; min_y = 0; min_w = min_view_w - min_x - 15 ; min_h = 30; self.titleLabel.frame = CGRectMake(min_x, min_y, min_w, min_h); self.titleLabel.zf_centerY = self.backBtn.zf_centerY;
min_h = iPhoneX ? 100 : 73; min_x = 0; min_y = min_view_h - min_h; min_w = min_view_w; self.bottomToolView.frame = CGRectMake(min_x, min_y, min_w, min_h);
min_x = (iPhoneX && self.player.orientationObserver.fullScreenMode == ZFFullScreenModeLandscape) ? 44: 15; min_y = 32; min_w = 30; min_h = 30; self.playOrPauseBtn.frame = CGRectMake(min_x, min_y, min_w, min_h);
min_x = self.playOrPauseBtn.zf_right + 4; min_y = 0; min_w = 62; min_h = 30; self.currentTimeLabel.frame = CGRectMake(min_x, min_y, min_w, min_h); self.currentTimeLabel.zf_centerY = self.playOrPauseBtn.zf_centerY;
min_w = 62; min_x = self.bottomToolView.zf_width - min_w - ((iPhoneX && self.player.orientationObserver.fullScreenMode == ZFFullScreenModeLandscape) ? 44: min_margin); min_y = 0; min_h = 30; self.totalTimeLabel.frame = CGRectMake(min_x, min_y, min_w, min_h); self.totalTimeLabel.zf_centerY = self.playOrPauseBtn.zf_centerY;
min_x = self.currentTimeLabel.zf_right + 4; min_y = 0; min_w = self.totalTimeLabel.zf_left - min_x - 4; min_h = 30; self.slider.frame = CGRectMake(min_x, min_y, min_w, min_h); self.slider.zf_centerY = self.playOrPauseBtn.zf_centerY;
min_x = (iPhoneX && self.player.orientationObserver.fullScreenMode == ZFFullScreenModeLandscape) ? 50: 18; min_y = 0; min_w = 40; min_h = 40; self.lockBtn.frame = CGRectMake(min_x, min_y, min_w, min_h); self.lockBtn.zf_centerY = self.zf_centerY;
if (!self.isShow) { self.topToolView.zf_y = -self.topToolView.zf_height; self.bottomToolView.zf_y = self.zf_height; } else { if (self.player.isLockedScreen) { self.topToolView.zf_y = -self.topToolView.zf_height; self.bottomToolView.zf_y = self.zf_height; } else { self.topToolView.zf_y = 0; self.bottomToolView.zf_y = self.zf_height - self.bottomToolView.zf_height; } } }
(void)makeSubViewsAction { [self.backBtn addTarget:self action:@selector(backBtnClickAction:) forControlEvents:UIControlEventTouchUpInside]; [self.playOrPauseBtn addTarget:self action:@selector(playPauseButtonClickAction:) forControlEvents:UIControlEventTouchUpInside]; [self.lockBtn addTarget:self action:@selector(lockButtonClickAction:) forControlEvents:UIControlEventTouchUpInside]; }
(void)layOutControllerViews { [self layoutIfNeeded]; [self setNeedsLayout]; }
(void)sliderTouchBegan:(float)value { self.slider.isdragging = YES; }
(void)sliderTouchEnded:(float)value { if (self.player.totalTime > 0) { if (self.videoTotalTime > 0) { @weakify(self) [self.player seekToTime:self.videoTotalTime value completionHandler:^(BOOL finished) { @strongify(self) if (finished) { self.slider.isdragging = NO; } }]; }else { @weakify(self) [self.player seekToTime:self.player.totalTime value completionHandler:^(BOOL finished) { @strongify(self) if (finished) { self.slider.isdragging = NO; } }]; }
if (self.seekToPlay) {
[self.player.currentPlayerManager play];
}
} else { self.slider.isdragging = NO; } if (self.sliderValueChanged) self.sliderValueChanged(value); }
(void)sliderValueChanged:(float)value { if (self.player.totalTime == 0) { self.slider.value = 0; return; } self.slider.isdragging = YES; NSString currentTimeString = [ZFUtilities convertTimeSecond:self.player.totalTime value]; if (self.videoTotalTime > 0) { currentTimeString = [ZFUtilities convertTimeSecond:self.videoTotalTime * value]; } self.currentTimeLabel.text = currentTimeString; if (self.sliderValueChanging) self.sliderValueChanging(value,self.slider.isForward); }
(void)sliderTapped:(float)value { if (self.player.totalTime > 0) { self.slider.isdragging = YES; if (self.videoTotalTime > 0) { @weakify(self) [self.player seekToTime:self.videoTotalTime value completionHandler:^(BOOL finished) { @strongify(self) if (finished) { self.slider.isdragging = NO; [self.player.currentPlayerManager play]; } }]; }else { @weakify(self) [self.player seekToTime:self.player.totalTime value completionHandler:^(BOOL finished) { @strongify(self) if (finished) { self.slider.isdragging = NO; [self.player.currentPlayerManager play]; } }]; }
} else { self.slider.isdragging = NO; self.slider.value = 0; } }
/// 重置ControlView
(void)resetControlView { self.slider.value = 0; self.slider.bufferValue = 0; self.currentTimeLabel.text = @"00:00"; self.totalTimeLabel.text = @"00:00"; self.backgroundColor = [UIColor clearColor]; self.playOrPauseBtn.selected = YES; self.titleLabel.text = @""; self.topToolView.alpha = 1; self.bottomToolView.alpha = 1; self.isShow = NO; }
(void)showControlView { self.lockBtn.alpha = 1; self.isShow = YES; if (self.player.isLockedScreen) { self.topToolView.zf_y = -self.topToolView.zf_height; self.bottomToolView.zf_y = self.zf_height; } else { self.topToolView.zf_y = 0; self.bottomToolView.zf_y = self.zf_height - self.bottomToolView.zf_height; } self.lockBtn.zf_left = iPhoneX ? 50: 18; self.player.statusBarHidden = NO; if (self.player.isLockedScreen) { self.topToolView.alpha = 0; self.bottomToolView.alpha = 0; } else { self.topToolView.alpha = 1; self.bottomToolView.alpha = 1; } }
(void)hideControlView { self.isShow = NO; self.topToolView.zf_y = -self.topToolView.zf_height; self.bottomToolView.zf_y = self.zf_height; self.lockBtn.zf_left = iPhoneX ? -82: -47; self.player.statusBarHidden = YES; self.topToolView.alpha = 0; self.bottomToolView.alpha = 0; self.lockBtn.alpha = 0; }
(BOOL)shouldResponseGestureWithPoint:(CGPoint)point withGestureType:(ZFPlayerGestureType)type touch:(nonnull UITouch *)touch { CGRect sliderRect = [self.bottomToolView convertRect:self.slider.frame toView:self]; if (CGRectContainsPoint(sliderRect, point)) { return NO; } if (self.player.isLockedScreen && type != ZFPlayerGestureTypeSingleTap) { // 锁定屏幕方向后只相应tap手势 return NO; } return YES; }
(void)videoPlayer:(ZFPlayerController *)videoPlayer presentationSizeChanged:(CGSize)size { self.lockBtn.hidden = self.player.orientationObserver.fullScreenMode == ZFFullScreenModePortrait; }
(void)videoPlayer:(ZFPlayerController )videoPlayer currentTime:(NSTimeInterval)currentTime totalTime:(NSTimeInterval)totalTime { if (!self.slider.isdragging) { NSString currentTimeString = [ZFUtilities convertTimeSecond:currentTime]; self.currentTimeLabel.text = currentTimeString; NSString *totalTimeString = [ZFUtilities convertTimeSecond:totalTime]; self.totalTimeLabel.text = totalTimeString;
if (self.videoTotalTime > 0) {
self.slider.value = currentTime/self.videoTotalTime;
}else {
self.slider.value = videoPlayer.progress;
}
} }
(void)videoPlayer:(ZFPlayerController *)videoPlayer bufferTime:(NSTimeInterval)bufferTime { if (self.videoTotalTime > 0) { self.slider.bufferValue = bufferTime/self.videoTotalTime; }else { self.slider.bufferValue = videoPlayer.bufferProgress; } }
(void)showTitle:(NSString *)title fullScreenMode:(ZFFullScreenMode)fullScreenMode { self.titleLabel.text = title; self.player.orientationObserver.fullScreenMode = fullScreenMode; self.lockBtn.hidden = fullScreenMode == ZFFullScreenModePortrait; }
/// 调节播放进度slider和当前时间更新
/// 滑杆结束滑动
(void)backBtnClickAction:(UIButton *)sender { self.lockBtn.selected = NO; self.player.lockedScreen = NO; self.lockBtn.selected = NO; if (self.player.orientationObserver.supportInterfaceOrientation & ZFInterfaceOrientationMaskPortrait) { [self.player enterFullScreen:NO animated:YES]; } if (self.backBtnClickCallback) { self.backBtnClickCallback(); } }
(void)playPauseButtonClickAction:(UIButton *)sender { [self playOrPause]; }
/// 根据当前播放状态取反
(void)playOrPause { self.playOrPauseBtn.selected = !self.playOrPauseBtn.isSelected; self.playOrPauseBtn.isSelected? [self.player.currentPlayerManager play]: [self.player.currentPlayerManager pause]; }
(void)playBtnSelectedState:(BOOL)selected { self.playOrPauseBtn.selected = selected; }
(void)lockButtonClickAction:(UIButton *)sender { sender.selected = !sender.selected; self.player.lockedScreen = sender.selected; }
(UIView )topToolView { if (!_topToolView) { _topToolView = [[UIView alloc] init]; UIImage image = ZFPlayer_Image(@"ZFPlayer_top_shadow"); _topToolView.layer.contents = (id)image.CGImage; } return _topToolView; }
(UIButton *)backBtn { if (!_backBtn) { _backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_backBtn setImage:ZFPlayer_Image(@"ZFPlayer_back_full") forState:UIControlStateNormal]; } return _backBtn; }
(UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.textColor = [UIColor whiteColor]; _titleLabel.font = [UIFont systemFontOfSize:15.0]; } return _titleLabel; }
(UIView )bottomToolView { if (!_bottomToolView) { _bottomToolView = [[UIView alloc] init]; UIImage image = ZFPlayer_Image(@"ZFPlayer_bottom_shadow"); _bottomToolView.layer.contents = (id)image.CGImage; } return _bottomToolView; }
(UIButton *)playOrPauseBtn { if (!_playOrPauseBtn) { _playOrPauseBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_playOrPauseBtn setImage:ZFPlayer_Image(@"ZFPlayer_play") forState:UIControlStateNormal]; [_playOrPauseBtn setImage:ZFPlayer_Image(@"ZFPlayer_pause") forState:UIControlStateSelected]; } return _playOrPauseBtn; }
(UILabel *)currentTimeLabel { if (!_currentTimeLabel) { _currentTimeLabel = [[UILabel alloc] init]; _currentTimeLabel.textColor = [UIColor whiteColor]; _currentTimeLabel.font = [UIFont systemFontOfSize:14.0f]; _currentTimeLabel.textAlignment = NSTextAlignmentCenter; } return _currentTimeLabel; }
(ZFSliderView *)slider { if (!_slider) { _slider = [[ZFSliderView alloc] init]; _slider.delegate = self; _slider.maximumTrackTintColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.8]; _slider.bufferTrackTintColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5]; _slider.minimumTrackTintColor = [UIColor whiteColor]; [_slider setThumbImage:ZFPlayer_Image(@"ZFPlayer_slider") forState:UIControlStateNormal]; _slider.sliderHeight = 2; } return _slider; }
(UILabel *)totalTimeLabel { if (!_totalTimeLabel) { _totalTimeLabel = [[UILabel alloc] init]; _totalTimeLabel.textColor = [UIColor whiteColor]; _totalTimeLabel.font = [UIFont systemFontOfSize:14.0f]; _totalTimeLabel.textAlignment = NSTextAlignmentCenter; } return _totalTimeLabel; }
(UIButton *)lockBtn { if (!_lockBtn) { _lockBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_lockBtn setImage:ZFPlayer_Image(@"ZFPlayer_unlock-nor") forState:UIControlStateNormal]; [_lockBtn setImage:ZFPlayer_Image(@"ZFPlayer_lock-nor") forState:UIControlStateSelected]; } return _lockBtn; }
@end
需求是 播放预览 但是总长时间需要显示整个视频的总长时间 但是预览的url是3分钟 实际完整视频27分钟这样子 所以需要自定义播放器的总长时间而不是获取URL里的