功能点罗列
- 播放,列表TableView,CollectionView,ScrollView播放
- 上下集切换
- 全屏
- 双击暂停
- 横竖屏切换
- 锁定旋转方向
- 状态栏样式、方向修改
- 播放完成、缓冲改变回调、加载状态改变
简单使用
#import <ZFPlayer/ZFPlayer.h>
#import <ZFPlayer/ZFAVPlayerManager.h>
#import <ZFPlayer/ZFIJKPlayerManager.h>
#import <ZFPlayer/KSMediaPlayerManager.h>
#import <ZFPlayer/ZFPlayerControlView.h>
#import "ZFSmallPlayViewController.h"
#import "UIImageView+ZFCache.h"
#import "ZFUtilities.h"
static NSString *kVideoCover = @"https://upload-images.jianshu.io/upload_images/635942-14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";
@interface ZFNoramlViewController ()
@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) UIImageView *containerView;
@property (nonatomic, strong) ZFPlayerControlView *controlView;
@end
/// 1.播放管理类
ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
/// 2.播放器相关
self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.containerView];
self.player.controlView = self.controlView;
/// 3.设置退到后台继续播放
self.player.pauseWhenAppResignActive = NO;
/// 4.方向发生变化时的回调
@weakify(self)
self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
@strongify(self)
//更新StatusBar方向
[self setNeedsStatusBarAppearanceUpdate];
};
/// 5.播放完成回调
self.player.playerDidToEnd = ^(id _Nonnull asset) {
@strongify(self)
[self.player.currentPlayerManager replay];
[self.player playTheNext];
if (!self.player.isLastAssetURL) {
NSString *title = [NSString stringWithFormat:@"视频标题%zd",self.player.currentPlayIndex];
[self.controlView showTitle:title coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeLandscape];
} else {
[self.player stop];
}
};
// 6.视频数组
self.player.assetURLs = self.assetURLs;
#pragma mark - 其他相关
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.player.viewControllerDisappear = NO;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.player.viewControllerDisappear = YES;
}
播放
[self.player playTheIndex:0];
[self.controlView showTitle:@"视频标题" coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeAutomatic];
切换Video
NSString *URLString = @"https://www.apple.com/105/media/cn/mac/family/2018/46c4b917_abfd_45a3_9b51_4e3054191797/films/bruce/mac-bruce-tpl-cn-2018_1280x720h.mp4";
//视频Url赋值
self.player.assetURL = [NSURL URLWithString:URLString];
[self.controlView showTitle:@"Apple" coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeAutomatic];
下一集
if (!self.player.isLastAssetURL) {
[self.player playTheNext];
NSString *title = [NSString stringWithFormat:@"视频标题%zd",self.player.currentPlayIndex];
[self.controlView showTitle:title coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeAutomatic];
} else {
NSLog(@"最后一个视频了");
}
其他配置项
/// 设置状态栏样式
- (UIStatusBarStyle)preferredStatusBarStyle {
if (self.player.isFullScreen) {
return UIStatusBarStyleLightContent;
}
return UIStatusBarStyleDefault;
}
/// 设置状态栏是否隐藏
- (BOOL)prefersStatusBarHidden {
/// 如果只是支持iOS9+ 那直接return NO即可,这里为了适配iOS8
return self.player.isStatusBarHidden;
}
///设置状态栏更新动画
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
return UIStatusBarAnimationSlide;
}
/// 是否允许界面旋转
- (BOOL)shouldAutorotate {
return self.player.shouldAutorotate;
}
/// 设置页面支持的旋转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if (self.player.isFullScreen) {
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
自定义控制层
/// 需要遵循 ZFPlayerMediaControl 协议 和 ZFSliderViewDelegate
/// Current playerController
@property (nonatomic, weak) ZFPlayerController *player;
#pragma mark - ZFPlayerControlViewDelegate
/// 手势筛选,返回NO不响应该手势
- (BOOL)gestureTriggerCondition:(ZFPlayerGestureControl *)gestureControl gestureType:(ZFPlayerGestureType)gestureType gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer touch:(nonnull UITouch *)touch {
CGPoint point = [touch locationInView:self];
if (self.player.isSmallFloatViewShow && !self.player.isFullScreen && gestureType != ZFPlayerGestureTypeSingleTap) {
return NO;
}
return [self shouldResponseGestureWithPoint:point withGestureType:gestureType touch:touch];
}
/// 单击手势事件
- (void)gestureSingleTapped:(ZFPlayerGestureControl *)gestureControl {
if (!self.player) return;
if (self.player.isSmallFloatViewShow && !self.player.isFullScreen) {
[self.player enterFullScreen:YES animated:YES];
} else {
if (self.controlViewAppeared) {
[self hideControlViewWithAnimated:YES];
} else {
/// 显示之前先把控制层复位,先隐藏后显示
[self hideControlViewWithAnimated:NO];
[self showControlViewWithAnimated:YES];
}
}
}
/// 双击手势事件
- (void)gestureDoubleTapped:(ZFPlayerGestureControl *)gestureControl {
[self playOrPause];
}
/// 开始滑动手势事件
- (void)gestureBeganPan:(ZFPlayerGestureControl *)gestureControl panDirection:(ZFPanDirection)direction panLocation:(ZFPanLocation)location {
if (direction == ZFPanDirectionH) {
self.sumTime = self.player.currentTime;
}
}
/// 滑动中手势事件
- (void)gestureChangedPan:(ZFPlayerGestureControl *)gestureControl panDirection:(ZFPanDirection)direction panLocation:(ZFPanLocation)location withVelocity:(CGPoint)velocity {
}
/// 滑动结束手势事件
- (void)gestureEndedPan:(ZFPlayerGestureControl *)gestureControl panDirection:(ZFPanDirection)direction panLocation:(ZFPanLocation)location {
@weakify(self)
if (direction == ZFPanDirectionH && self.sumTime >= 0 && self.player.totalTime > 0) {
[self.player seekToTime:self.sumTime completionHandler:^(BOOL finished) {
@strongify(self)
/// 左右滑动调节播放进度
[self sliderChangeEnded];
if (self.controlViewAppeared) {
[self autoFadeOutControlView];
}
}];
self.sumTime = 0;
}
}
/// 捏合手势事件,这里改变了视频的填充模式
- (void)gesturePinched:(ZFPlayerGestureControl *)gestureControl scale:(float)scale {
if (scale > 1) {
self.player.currentPlayerManager.scalingMode = ZFPlayerScalingModeAspectFill;
} else {
self.player.currentPlayerManager.scalingMode = ZFPlayerScalingModeAspectFit;
}
}
/// 准备播放
- (void)videoPlayer:(ZFPlayerController *)videoPlayer prepareToPlay:(NSURL *)assetURL {
[self hideControlViewWithAnimated:NO];
}
/// 播放状态改变
- (void)videoPlayer:(ZFPlayerController *)videoPlayer playStateChanged:(ZFPlayerPlaybackState)state {
if (state == ZFPlayerPlayStatePlaying) {
[self playBtnSelectedState:YES];
/// 开始播放时候判断是否显示loading
if (videoPlayer.currentPlayerManager.loadState == ZFPlayerLoadStateStalled) {
[self.activity startAnimating];
} else if ((videoPlayer.currentPlayerManager.loadState == ZFPlayerLoadStateStalled || videoPlayer.currentPlayerManager.loadState == ZFPlayerLoadStatePrepare)) {
[self.activity startAnimating];
}
} else if (state == ZFPlayerPlayStatePaused) {
[self playBtnSelectedState:NO];
/// 暂停的时候隐藏loading
[self.activity stopAnimating];
} else if (state == ZFPlayerPlayStatePlayFailed) {
[self.activity stopAnimating];
}
}
/// 加载状态改变
- (void)videoPlayer:(ZFPlayerController *)videoPlayer loadStateChanged:(ZFPlayerLoadState)state {
if (state == ZFPlayerLoadStatePrepare) {
self.coverImageView.hidden = NO;
} else if (state == ZFPlayerLoadStatePlaythroughOK || state == ZFPlayerLoadStatePlayable) {
self.coverImageView.hidden = YES;
self.player.currentPlayerManager.view.backgroundColor = [UIColor blackColor];
}
if (state == ZFPlayerLoadStateStalled && videoPlayer.currentPlayerManager.isPlaying) {
[self.activity startAnimating];
} else if ((state == ZFPlayerLoadStateStalled || state == ZFPlayerLoadStatePrepare) && videoPlayer.currentPlayerManager.isPlaying) {
[self.activity startAnimating];
} else {
[self.activity stopAnimating];
}
}
/// 播放进度改变回调
- (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;
self.slider.value = videoPlayer.progress;
}
self.bottomPgrogress.value = videoPlayer.progress;
}
/// 缓冲改变回调
- (void)videoPlayer:(ZFPlayerController *)videoPlayer bufferTime:(NSTimeInterval)bufferTime {
self.slider.bufferValue = videoPlayer.bufferProgress;
self.bottomPgrogress.bufferValue = videoPlayer.bufferProgress;
}
- (void)videoPlayer:(ZFPlayerController *)videoPlayer presentationSizeChanged:(CGSize)size {
}
/// 视频view即将旋转
- (void)videoPlayer:(ZFPlayerController *)videoPlayer orientationWillChange:(ZFOrientationObserver *)observer {
if (videoPlayer.isSmallFloatViewShow) {
if (observer.isFullScreen) {
self.controlViewAppeared = NO;
[self cancelAutoFadeOutControlView];
}
}
if (self.controlViewAppeared) {
[self showControlViewWithAnimated:NO];
} else {
[self hideControlViewWithAnimated:NO];
}
}
/// 视频view已经旋转
- (void)videoPlayer:(ZFPlayerController *)videoPlayer orientationDidChanged:(ZFOrientationObserver *)observer {
if (self.controlViewAppeared) {
[self showControlViewWithAnimated:NO];
} else {
[self hideControlViewWithAnimated:NO];
}
[self layoutIfNeeded];
[self setNeedsDisplay];
}
/// 锁定旋转方向
- (void)lockedVideoPlayer:(ZFPlayerController *)videoPlayer lockedScreen:(BOOL)locked {
[self showControlViewWithAnimated:YES];
}
#pragma mark - ZFSliderViewDelegate
- (void)sliderTouchBegan:(float)value {
self.slider.isdragging = YES;
}
- (void)sliderTouchEnded:(float)value {
if (self.player.totalTime > 0) {
@weakify(self)
[self.player seekToTime:self.player.totalTime*value completionHandler:^(BOOL finished) {
@strongify(self)
if (finished) {
self.slider.isdragging = NO;
}
}];
} else {
self.slider.isdragging = NO;
}
}
- (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];
self.currentTimeLabel.text = currentTimeString;
}
- (void)sliderTapped:(float)value {
if (self.player.totalTime > 0) {
self.slider.isdragging = YES;
@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;
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。