iOS--视频播放器之ZFPlayer

article/2025/8/28 0:57:06

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

还是直接上代码,详情如下:

一.ZFPlayer的导入。pod 'ZFPlayer',头文件 #import "ZFPlayer.h"

二.在cell中使用图片看得更清晰,代码可以直接复制

图片:  .h文件

.m文件

代码:

//
//  CellShowController.h
//  TestZFPlayer
//
//  Created by 黄亚男 on 2018/2/23.
//  Copyright © 2018年 黄亚男. All rights reserved.
//#import <UIKit/UIKit.h>#import "ZFPlayer.h"
@interface CellShowController : UIViewController+ (instancetype)sharedInstance ;@property (nonatomic, strong) ZFPlayerView *playerView;@end
//
//  CellShowController.m
//  TestZFPlayer
//
//  Created by 黄亚男 on 2018/2/23.
//  Copyright © 2018年 黄亚男. All rights reserved.
//›#import "CellShowController.h"
#import "VideoCell.h"@interface CellShowController ()<UITableViewDelegate,UITableViewDataSource,ZFPlayerDelegate>@property (nonatomic, strong) UITableView *tbView;
//视频数据
@property (nonatomic,strong) NSArray *videoData;
//图片数据
@property (nonatomic,strong) NSArray *coverUrl;@property (nonatomic, strong) ZFPlayerControlView *controlView;@end@implementation CellShowController
+ (instancetype)sharedInstance {static CellShowController *_sharedVideoVC = nil;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{_sharedVideoVC = [[CellShowController alloc] init];_sharedVideoVC.playerView = [[ZFPlayerView alloc] init];});return _sharedVideoVC;
}
//视频数据
-(NSArray *)videoData
{if(!_videoData){//网络数据,这里为假数据_videoData = [NSArray arrayWithObjects:@"1.mp4",@"2.mp4",@"3.mp4",@"4.mp4",@"5.mp4", nil];}return _videoData;
}
//图片数据
-(NSArray *)coverUrl
{if(!_coverUrl){_coverUrl = [NSArray arrayWithObjects:@"1.jpg",@"2.png",@"3.jpg",@"4.jpg",@"5.jpg", nil];}return _coverUrl;
}-(void)viewWillAppear:(BOOL)animated
{[super viewWillAppear:animated];ZFPlayerShared.isStatusBarHidden = NO;}
// 页面消失时候
- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:animated];[self.playerView resetPlayer];}- (UIStatusBarStyle)preferredStatusBarStyle {// 这里设置横竖屏不同颜色的statusbarif (ZFPlayerShared.isLandscape) {return UIStatusBarStyleLightContent;}return UIStatusBarStyleDefault;
}- (BOOL)prefersStatusBarHidden {//ZFPlayerShared.isStatusBarHiddenreturn ZFPlayerShared.isStatusBarHidden;
}
- (void)viewDidLoad {[super viewDidLoad];self.navigationItem.title = @"cell中播放";[self createTableView];
}-(void)createTableView
{_tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, KSCREEN_WIDTH, KSCREEN_HEIGHT-20) style:UITableViewStyleGrouped];_tbView.delegate = self;_tbView.dataSource = self;//隐藏cell的分割线_tbView.separatorStyle = UITableViewCellSeparatorStyleNone;_tbView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];_tbView.estimatedRowHeight = 0;_tbView.estimatedSectionHeaderHeight = 0;_tbView.estimatedSectionFooterHeight = 0;if (@available(iOS 11.0, *)) {UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;_tbView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;}[self.view addSubview:self.tbView];
}#pragma mark UITableViewDataSource---分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{return self.videoData.count;}
#pragma mark UITableViewDataSource---行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{return 1;
}
#pragma mark UITableViewDataSource---行单元
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{VideoCell *cell = [VideoCell cellWithTableView:tableView];[cell.headerImage sd_setImageWithURL:[NSURL URLWithString:self.coverUrl[indexPath.section]]];__block NSIndexPath *weakIndexPath = indexPath;__block VideoCell *weakCell  = cell;__weak typeof(self)  weakSelf = self;cell.viewBlock = ^(UIView *view) {ZFPlayerModel *playerModel = [[ZFPlayerModel alloc] init];playerModel.title            = @"";//本地播放// 文件存放路径//        NSString *path                   = FILE_PATH(model.fileName);//        NSURL *videoURL                  = [NSURL fileURLWithPath:path];//网络播放playerModel.videoURL         = [NSURL URLWithString:self.videoData[indexPath.section]];playerModel.scrollView = weakSelf.tbView;playerModel.indexPath = weakIndexPath;// 赋值分辨率字典//playerModel.resolutionDic    = dic;// player的父视图tagplayerModel.fatherViewTag = weakCell.headerImage.tag;weakSelf.playerView.delegate = self;// 设置播放控制层和model[weakSelf.playerView playerControlView:nil playerModel:playerModel];// 下载功能//weakSelf.playerView.hasDownload = YES;// 自动播放[weakSelf.playerView autoPlayTheVideo];};return cell;}
#pragma mark UITableViewDelegate---点击行单元调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark UITableViewDelegate---表头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{return 10;
}
#pragma mark UITableViewDelegate---表位高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{return 0.01;
}
#pragma mark UITableViewDelegate---行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{return 250*SCREEN_WIDTH_RATIO47;
}#pragma ================视频播放==================
- (ZFPlayerView *)playerView {if (!_playerView) {_playerView = [ZFPlayerView sharedPlayerView];_playerView.delegate = self;//(可选设置)可以设置视频的填充模式_playerView.playerLayerGravity = ZFPlayerLayerGravityResizeAspect;// 当cell播放视频由全屏变为小屏时候,不回到中间位置// _playerView.cellPlayerOnCenter = NO;// 当cell划出屏幕的时候停止播放_playerView.stopPlayWhileCellNotVisable = YES;// 静音// _playerView.mute = YES;//打开预览图self.playerView.hasPreviewView = YES;}return _playerView;
}
- (ZFPlayerControlView *)controlView {if (!_controlView) {_controlView = [[ZFPlayerControlView alloc] init];_controlView.backgroundColor = [UIColor clearColor];}return _controlView;
}@end

 

//
//  VideoCell.h
//  TestZFPlayer
//
//  Created by 黄亚男 on 2018/2/23.
//  Copyright © 2018年 黄亚男. All rights reserved.
//#import <UIKit/UIKit.h>typedef void(^viewBlock)(UIView *view);@interface VideoCell : UITableViewCell@property(nonatomic, copy) viewBlock viewBlock;/**占位图*/
@property (nonatomic, strong) UIImageView *headerImage;
/**播放按钮*/
@property (nonatomic, strong) UIButton *playerButton;+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end
//
//  VideoCell.m
//  TestZFPlayer
//
//  Created by 黄亚男 on 2018/2/23.
//  Copyright © 2018年 黄亚男. All rights reserved.
//#import "VideoCell.h"@implementation VideoCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self)    {//创建UI[self createUI];}return self;}
//创建UI
-(void)createUI{CGFloat originX,originY,width,hight;//背景图片originX = 0;originY = 0;width = KSCREEN_WIDTH;hight = 250*SCREEN_WIDTH_RATIO47;UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(originX, originY, width, hight)];_headerImage = bgView;_headerImage.tag = 20030;_headerImage.userInteractionEnabled = YES;[self.contentView addSubview:bgView];//播放按钮originX = KSCREEN_WIDTH/2-25;width = 50;originY = 100*SCREEN_WIDTH_RATIO47;UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];btn.frame = CGRectMake(originX, originY, width, width);[btn setBackgroundImage:[UIImage imageNamed:@"viedo_stop_icon"] forState:UIControlStateNormal];_playerButton = btn;btn.tag = 2005;[btn addTarget:self action:@selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];[_headerImage addSubview:btn];}//视频播放
-(void)playVideo:(UIButton *)sender
{if (self.viewBlock) {_viewBlock(sender);}
}
+ (instancetype)cellWithTableView:(UITableView *)tableView{static NSString *identify = @"VideoCellID";VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];if (cell == nil) {cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];}    return cell;}@end

效果图:

    

 

 

三.在View中,这个比较简单

上图:

代码:

//
//  ViewShowController.m
//  TestZFPlayer
//
//  Created by 黄亚男 on 2018/2/24.
//  Copyright © 2018年 黄亚男. All rights reserved.
//#import "ViewShowController.h"@interface ViewShowController () <ZFPlayerDelegate>@property (strong, nonatomic) ZFPlayerView *playerView;
/** 播放器View的父视图*/
@property (strong, nonatomic) UIImageView *fatherView;@property (nonatomic, strong) ZFPlayerModel *playerModel;@end@implementation ViewShowController-(void)viewWillAppear:(BOOL)animated
{[super viewWillAppear:animated];ZFPlayerShared.isStatusBarHidden = NO;}
// 页面消失时候
- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:animated];[self.playerView resetPlayer];}- (UIStatusBarStyle)preferredStatusBarStyle {// 这里设置横竖屏不同颜色的statusbarif (ZFPlayerShared.isLandscape) {return UIStatusBarStyleLightContent;}return UIStatusBarStyleDefault;
}- (void)viewDidLoad {[super viewDidLoad];[self createUI];}-(void)createUI
{UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, KSCREEN_WIDTH, 250*SCREEN_WIDTH_RATIO47)];_fatherView = bgView;_fatherView.tag = 10090;_fatherView.userInteractionEnabled = YES;//图片网址,为假数据[_fatherView sd_setImageWithURL:[NSURL URLWithString:@"http://2.jpg"]];[self.view addSubview:bgView];//播放按钮UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];btn.frame = CGRectMake(KSCREEN_WIDTH/2-25, 100*SCREEN_WIDTH_RATIO47,50, 50);[btn setBackgroundImage:[UIImage imageNamed:@"viedo_stop_icon"] forState:UIControlStateNormal];btn.tag = 2005;[btn addTarget:self action:@selector(playViewVideo:) forControlEvents:UIControlEventTouchUpInside];[_fatherView addSubview:btn];}- (ZFPlayerModel *)playerModel {if (!_playerModel) {_playerModel                  = [[ZFPlayerModel alloc] init];_playerModel.title            = @"这里设置视频标题";_playerModel.placeholderImage = [UIImage imageNamed:@"loading_bgView1"];_playerModel.fatherView       = self.fatherView;}return _playerModel;
}
- (ZFPlayerView *)playerView {if (!_playerView) {_playerView = [[ZFPlayerView alloc] init];[_playerView playerControlView:nil playerModel:self.playerModel];// 设置代理_playerView.delegate = self;//(可选设置)可以设置视频的填充模式,内部设置默认(ZFPlayerLayerGravityResizeAspect:等比例填充,直到一个维度到达区域边界)// _playerView.playerLayerGravity = ZFPlayerLayerGravityResize;// 打开下载功能(默认没有这个功能)_playerView.hasDownload    = YES;// 打开预览图_playerView.hasPreviewView = YES;//        _playerView.forcePortrait = YES;/// 默认全屏播放//        _playerView.fullScreenPlay = YES;}return _playerView;
}
#pragma mark -点击按钮播放
-(void)playViewVideo:(UIButton *)btn
{_playerModel.videoURL         = [NSURL URLWithString:@"http://1.mp4"];//网络地址(为假链接)_playerModel.placeholderImage = [UIImage imageNamed:@"1"];[self.playerView resetToPlayNewVideo:self.playerModel];}#pragma mark - ZFPlayerDelegate//小屏时返回按钮
//- (void)zf_playerBackAction
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}
//下载按钮
- (void)zf_playerDownload:(NSString *)url
{NSLog(@"0000");
}@end

效果图:

    

 

写的比较匆忙,不足之处,请留言

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/huangyn/blog/1625274


http://chatgpt.dhexx.cn/article/I8OM7DeB.shtml

相关文章

ios 按位运算---分解ZFPlayer

按位与&&#xff08;位运算时&#xff0c;相同为1&#xff0c;不同为0&#xff09; 按位或 |&#xff08;位运算时&#xff0c;有1为1&#xff0c;全0为0&#xff09; 按位异或 ^&#xff08;位运算时&#xff0c;相同为0&#xff0c;不同为1&#xff09; 按位取反~&#x…

ios 视频列表处理---分解ZFPlayer

1.视频播放器添加到containerView的机制与一个普通播放器页面的不同 普通视频播放页面可以直接添加一个播放器&#xff0c;按照正常逻辑播放、暂停、切换等操作&#xff0c;而视频列表的做法是 用户触发播放动作 当点击一个cell上的播放按钮时&#xff0c;首先判断当前是否有其…

iOS16灵动岛横屏视频播放适配(ZFPlayer)

项目场景&#xff1a; 手机为iphone14Pro 版本iOS16.0.3 Xcode版本14.2 视频播放第三方库ZFPlayer 问题描述 使用视频时&#xff0c;视频播放自动横屏控制层的返回按钮和暂停按钮都点不到&#xff0c;上图错误、下图正确&#xff08;控制按钮距离屏幕左右减小50、视频全屏不做…

ZFPlayer视频播放PC 安卓都可以正常 播放,iOS端只有声音没有画面

1.添加视频&#xff0c;选择MP4格式 2.设置输出配置&#xff0c;并选择输出文件夹&#xff0c;视频编码为AVC(H264)&#xff0c;屏幕大小调整为720*480 3&#xff0c;点击开始 视频会出现在指定文件夹。可配置自定义文件夹 用格式工厂帮忙转个格式可以得出能播放和不能播放的区…

ZFPlayer 源码解读

源码下载地址&#xff1a;https://github.com/renzifeng/ZFPlayer 之前自己实现过一个模仿百思不得姐的demo https://github.com/agelessman/FFmpegAndKxmovieDemo 由于有朋友推荐&#xff0c;看了下ZFPlayer&#xff0c;觉得功能和封装都写的很好&#xff0c;就把源码看了一遍…

iOS开发-ZFPlayer的简单使用 播放单个网络视频

iOS开发-ZFPlayer的简单使用 播放单个网络视频 前言开发准备代码注意 前言 关于ZFPlayer播放单个网络视频案例&#xff0c;它的网络列表视频案例在gitHub上面很多。 开发准备 podfile导入 pod ZFPlayer, ~> 3.3.3 #, ~> 3.2.17 # 视频播放 pod ZFPlayer/ControlView p…

关于ZFPlayer集成播放 rtmp 视频流的总结

最近公司项目需要能够播放 rtmp 格式的直播流地址的播放器,安卓那边是买了人家的三方库,EasyPlayer,但是不想再买一个 ios 的了所以需要自己去找免费的合适的播放器去集成,终于在 github 上找到一个合适的了,那就是 强大的IJKPlayer 播放器;但在集成的时候出现了一些问题,朋友推…

ZFPlayer 播放器调用addPlayerViewToSmallFloatView开小窗播放样式错误以及无法关闭处理

ZFPLayer 是一款强大开源的视频播放器&#xff0c;在结合列表播放滑动超出当前视频范围调用 addPlayerViewToSmallFloatView开启小窗播放(类似画中画&#xff0c;但没有其强大和友好的交互)布局错误并且右上角的关闭按钮也没法关闭&#xff0c;错乱参考图如下&#xff1a; 1、样…

ios html zfplayer,【iOS】ZFPlayer源码解读中

前言 本篇继ZFPlayer源码解读基础之上&#xff0c;主要解析说明控制层与播放器&#xff0c;因为在上篇文章至现在并未提及丝毫关于这两个类业务的实现。 首先说下这两个类各自的职责。 控制层&#xff1a;主要负责响应与用户之间的交互&#xff0c;如手势控制的播放&#xff0c…

ZFPlayer iOS16 系统横屏全屏问题处理

iOS16 以后 通过原始横屏的方法已经不好使了&#xff0c;需要在此基础上添加 setNeedsUpdateOfSupportedInterfaceOrientations 方法的调用&#xff0c;具体API 描述如下&#xff1a; 回到主题&#xff0c;在使用ZFPlayer 视频库全屏播放时iOS16系统下的处理参考如下&#xff…

iOS使用ZFPlayer 实现视频播放

文章目录 ZFPlayer 视频播放使用1 创建 ZFAVPlayerManager 对象2创建containerView, 也就是视频视图的父视图3 创建 controllView4 创建 ZFPlayerController播放视频判断视频的横竖 ZFPlayer 视频播放使用 1 创建 ZFAVPlayerManager 对象 ZFAVPlayerManager *manager [[ZFAVP…

配置分离式LANP源代码编译实例

一、php编译 配置apache-2.4.9以fpm方式的php-5.4.26&#xff08;php服务器IP为172.16.3.30&#xff09; 1、编译php #tar xf php-5.4.26.tar.bz2 #cd php-5.4.26 # ./configure --prefix/usr/local/php --with-mysql/usr/l…

Centos使用Docker搭建Lanp环境

前言 LAMP 指的Linux(操作系统)、ApacheHTTP服务器&#xff0c;MySQL(有时也指MariaDB&#xff0c;数据库软件) 和 PHP(有时也是指Perl或Python)的第一个字母&#xff0c;集成环境方便&#xff0c;一般用来建立web应用平台 Docker 是一个开源的应用容器引擎&#xff0c;在doc…

LANP+KEEPALIVED集群(一)

lanpkeepalived集群 1、nginx模块与工作原理 (1)结构上分 基础模块&#xff1a;HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite 核心模块&#xff1a;HTTP模块、EVENT模块和MAIL模块 第三方模块&#xff1a;HTTP Upstream Request Hase模块、Notice模块和HT…

LANP+KEEPALIVED集群(二)

LANPKEEPALIVED集群&#xff08;二&#xff09; #基于不同域名 server { listen 80; server_name nginx.postfix.local; charset utf-8; access_log logs/domain.log main; location / { root html/domain; index domain.html; } error_page 500 502 503 504 /5…

LWIP网络协议基础

1、LWIP有3种编程接口&#xff1a;RAW&#xff08;裸机跑&#xff0c;不带操作系统&#xff09;、NETCONN和SOCKET(要带操作系统) 例程使用的是ucos小型操作系统&#xff0c;ucosii任务数限制了最大只能有255个任务&#xff08;其中0—空闲任务和254、255—系统任务不能用&…

LAN IP,WAN IP和Global IP

LAN IP&#xff0c;WAN IP和Global IP是与网络通信相关的三种IP地址&#xff0c;下面是它们的解释&#xff1a; LAN IP 局域网IP地址&#xff08;LAN IP&#xff09;&#xff0c;又称私有IP地址&#xff0c;是指对于一个私有网络环境内的主机所使用的IP地址。这些IP地址属于私…

一文读懂ssh,tomcat,LANP,LNMP,ftp,dns等常见的环境配置(运维工程师必看)

文章目录 一.网络服务的概述1.网络服务是什么2.网络服务有哪些&#xff08;进行简单的梳理&#xff09;3.网络服务学习建议 二.网络服务基础1.CentOS6与7的区别2.常见网络端口以及/etc/service文件3.网关和路由,主机名&#xff08;路由选择&#xff0c;网关&#xff0c;NAT解释…

ubuntu 上搭建lanp环境

2019独角兽企业重金招聘Python工程师标准>>> 1.安装tasksel sudo apt-get install tasksel 使用tasksel 时只需 sudo tasksel 2.安装lamp sudo tasksel instal lamp-server 打开浏览器输入127.0.0.1 可以看到apache首页 切换到/var/www/html(默认目录) 目录下新…

LANP环境编译设置

1.下载nginx-1.10.2 wget -O nginx-1.10.2.tar.gz http://nginx.org/download/nginx-1.10.2.tar.gz 2.安装nginx-1.10.2.tar.gz [rootxuegod64 ~]# yum in stall -y gcc gcc-c autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel //zlib&#xff1a;给Ngin…