未优化短视频 | 优化后短视频 |
| |
pod 'TUIPlayerShortVideo' ,:path => '../../../SDK/TUIPlayerShortVideoSDK/'pod 'TUIPlayerCore' ,:path => '../../../SDK/TUIPlayerCoreSDK/'
- [AppDelegate application:didFinishLaunchingWithOptions:]
中,做如下配置:NSString * const licenceURL = @"<获取到的licenseUrl>";NSString * const licenceKey = @"<获取到的key>";[TXLiveBase setLicenceURL:licenceUrl key:licenceKey];[[TXLiveBase sharedInstance] setDelegate:self];
- (TUIShortVideoView *)videoView {if (!_videoView) {///设置自定义UITUIPlayerShortVideoUIManager *uiManager = [[TUIPlayerShortVideoUIManager alloc] init];[uiManager setControlViewClass: TUIPlayerShortVideoControlView.class];[uiManager setControlViewClass: TUIPSControlLiveView.class viewType:TUI_ITEM_VIEW_TYPE_LIVE];[uiManager setControlViewClass: TUIPSControlCustomView.class viewType:TUI_ITEM_VIEW_TYPE_CUSTOM];[uiManager setLoadingView:[[TUIPSDLoadingView alloc] init]];_videoView = [[TUIShortVideoView alloc] initWithUIManager:uiManager];_videoView.delegate = self;_videoView.customCallbackDelegate = self;//_videoView.isAutoPlay = NO;// Set your playback strategyTUIPlayerVodStrategyModel *model = [[TUIPlayerVodStrategyModel alloc] init];model.mPreloadConcurrentCount = 1;model.preDownloadSize = 1;model.enableAutoBitrate = NO;// live strategyTUIPlayerLiveStrateyModel *liveStrateyModel = [[TUIPlayerLiveStrateyModel alloc] init];[_videoView setShortVideoLiveStrategyModel:liveStrateyModel];}return _videoView;}
videoView.frame = self.view.bounds;[self.view addSubview:self.videoView];
TUIPlayerVideoModel *model1 = [[TUIPlayerVideoModel alloc] init]; ///视频数据TUIPlayerLiveModel *model2 = [[TUIPlayerLiveModel alloc] init]; ///直播数据TUIPlayerDataModel *model3 = [[TUIPlayerDataModel alloc] init]; ///自定义数据/// 这里根据您的业务情况,自行决定每一页的数据量NSArray *videos1 = @[model1,model2,model3];[self.videoView setShortVideoModels:videos1];
TUIPlayerVideoModel *model1 = [[TUIPlayerVideoModel alloc] init]; ///视频数据TUIPlayerLiveModel *model2 = [[TUIPlayerLiveModel alloc] init]; ///直播数据TUIPlayerDataModel *model3 = [[TUIPlayerDataModel alloc] init]; ///自定义数据/// 这里根据您的业务情况,自行决定每一页的数据量NSArray *videos2 = @[model1,model2,model3];-(void)onReachLast {///这里您可以做数据index索引记录,继续拼入您的第 3 4 5 6....组数据[self.videoView appendShortVideoModels:videos2];}
参数名称 | 含义 |
isAutoPlay | 首次加载是否自动播放第一个视频,默认 YES |
videos | 只读属性,获取当前存在与视频列表中的数据 |
currentVideoModel | 当前正在播放的视频模型 |
currentVideoIndex | 当前正在播放的视频索引 |
currentPlayerStatus | 当前播放器的播放状态 |
isPlaying | 当前播放器是否正在播放 |
delegate | 代理 |
refreshControl | 设置下拉刷新控件 |
initWithUIManager | 初始化(带自定义 UI) |
setShortVideoStrategyModel | 设置直播播放策略 |
setShortVideoLiveStrategyModel | 设置直播播放策略 |
setShortVideoModels | 首次设置数据源 |
appendShortVideoModels | 追加视频数据源 |
removeAllVideoModels | 删除所有视频数据 |
setPlaymode | 视频播放模式,单个循环或列表循环,默认前者 |
pause | 暂停 |
resume | 继续播放 |
destoryPlayer | 销毁播放器 |
didScrollToCellWithIndex | 跳到指定索引的视频 |
startLoading | 展示 loading 图 |
stopLoading | 隐藏 loading 图 |
currentPlayerSupportedBitrates | 当前正在播放的视频支持的码率 |
bitrateIndex | 获取当前正在播放的码率索引 |
switchResolution:index: | 切换分辨率 |
pausePreload | 暂停预加载 |
resumePreload | 恢复预加载 |
getDataManager | 获取数据管理器 |
getVodStrategyManager | 获取点播策略管理器 |
getLiveStrategyManager | 获取直播策略管理器 |
参数名称 | 含义 |
enableLog | 是否允许打印日志,默认 NO |
TUIPlayerConfig *config = [TUIPlayerConfig new];config.enableLog = YES;[[TUIPlayerCore shareInstance] setPlayerConfig:config];
参数名称 | 含义 |
mPreloadConcurrentCount | 视频缓存个数,默认3 |
mPreloadBufferSizeInMB | 预播放大小,单位 MB,默认0.5MB |
preferredResolution | 偏好分辨率,默认720 * 1280 |
progressInterval | 进度条回调间隔时长,单位毫秒,默认500ms |
renderMode | 画布填充样式,默认图像适应屏幕,保持画面完整 |
extInfoMap | 额外参数,预留 |
enableAutoBitrate | 是否开启自适应码率,默认 NO |
mediaType | 设置媒资类型 |
maxBufferSize | 最大预加载大小,单位 MB ,默认10MB,此设置会影响 playableDuration,设置越大,提前缓存的越多 |
mResumeModel | 续播模式,默认 TUI_RESUM_MODEL_NONE |
preDownloadSize | 预下载大小,单位 MB,默认1MB |
enableAccurateSeek | 是否精确 seek,默认 YES。开启精确后 seek,seek 的时间平均多出 200ms |
audioNormalization
| 音量均衡。响度范围:-70~0(LUFS)。此配置需要 LiteAVSDK 11.7 及以上版本支持。
以下几种常量供参考使用: 关:AUDIO_NORMALIZATION_OFF (TXVodPlayConfig.h) 开(标准响度):AUDIO_NORMALIZATION_STANDARD (TXVodPlayConfig.h) 开(低响度):AUDIO_NORMALIZATION_LOW (TXVodPlayConfig.h) 开(高响度):AUDIO_NORMALIZATION_HIGH (TXVodPlayConfig.h)
默认值为 AUDIO_NORMALIZATION_OFF。 |
isLastPrePlay | 是否保留上一个预播放,默认 NO |
subtitleRenderModel | 字幕样式 |
TUIPlayerStrategyModel *model = [[TUIPlayerStrategyModel alloc] init];model.mPreloadConcurrentCount = 1;model.preDownloadSize = 1;model.enableAutoBitrate = NO;model.mRenderMode = TUI_RENDER_MODE_FILL_SCREEN;model.mResumeModel = TUI_RESUM_MODEL_LAST;[_videoView setShortVideoStrategyModel:model];
参数名称 | 含义 |
isLastPrePlay | 是否保留上一个预播放,默认 NO |
mRenderMode | 画布填充样式,默认 V2TXLiveFillModeFill |
enablePictureInPicture | YES:开启画中画功能;NO:关闭画中画功能。默认值:NO。 |
volume | 播放器音量,取值范围0 - 100。默认值:100。 |
maxAutoAdjustCacheTime | 播放器缓存自动调整的最大时间,单位秒,取值需要大于0,默认值:5。 |
minAutoAdjustCacheTime | 播放器缓存自动调整的最小时间,单位秒,取值需要大于0,默认值为1。 |
isShowDebugView | 是否显示播放器状态信息的调试浮层默认值:NO。 |
TUIPlayerLiveStrategyModel *liveStrategyModel = [[TUIPlayerLiveStrategyModel alloc] init];[_videoView setShortVideoLiveStrategyModel:liveStrategyModel];
TUIPlayerVodStrategyManager *VodStrategyManager = [_videoView getVodStrategyManager]TUIPlayerVodStrategyManager *LiveStrategyManager = [_videoView getLiveStrategyManager]
[VodStrategyManager setRenderMode:TUI_RENDER_MODE_FILL_EDGE];[LiveStrategyManager setRenderMode:V2TXLiveFillModeFill];
参数说明 | 含义 |
TUIPlayerDataModel | 基本的数据类型 |
TUIPlayerVideoModel | 点播数据类型,继承于 TUIPlayerDataModel |
TUIPlayerLiveModel | 直播数据类型,继承于 TUIPlayerDataModel |
参数说明 | 含义 |
modelType | 模型类型 |
extInfo | extInfo 数据发生改变的 block |
onExtInfoChangedBlock | 直播数据类型 |
extInfoChangeNotify | 通知 extInfo 数据发生改变 |
asVodModel | 强转为 TUIPlayerVideoModel 类型 |
asLiveModel | 强转为 TUIPlayerLiveModel 类型 |
参数说明 | 含义 |
videoUrl | 视频 Url 地址 |
coverPictureUrl | 封面图 |
duration | 视频时长 |
appId | appid |
fileId | 视频的 fileId |
pSign | 签名字串 |
subtitles | 字幕信息 |
config | 视频的单独配置,详情请看 TUIPlayerVideoConfig |
参数说明 | 含义 |
liveUrl | 直播 Url |
coverPictureUrl | 封面图 |
TUIPlayerVideoModel *model = [[TUIPlayerVideoModel alloc] init];model.videoUrl = @"xxxx";model.coverPictureUrl = @"xxxx";model.duration = @"xxxx";model.appId = @"xxxx";model.fileId = @"xxxx";model.pSign = @"xxxx";NSDictionary *extr = @{@"name":@"@Mars",@"titile":@"This is a vod broadcast interface",@"des":@"This is a vod broadcast interface"};model.extInfo = extr;[modelArray addObject:model];
TUIPlayerLiveModel *model = [[TUIPlayerLiveModel alloc] init];model.liveUrl = @"xxxx";model.coverPictureUrl = @"xxxx";NSDictionary *extr = @{@"name":@"@Mars",@"liveTitile":@"This is a live broadcast interface",@"liveDes":@"This is a live broadcast interface"};model.extInfo = extr;
/// 1 轮播图TUIPlayerDataModel *model = [[TUIPlayerDataModel alloc] init];NSDictionary *extr = @{@"images":@"xxxx",@"url":@"https://cloud.tencent.com",@"titile":@"This is a picture carousel display interface",@"des":@"This is a picture carousel display interface",@"name":@"@Mars",@"type":@"imageCycle"};model.extInfo = extr;[modelArray insertObject:model atIndex:1];/// 2 图文广告TUIPlayerDataModel *model1 = [[TUIPlayerDataModel alloc] init];NSDictionary *extr1 = @{@"adUrl":@"https://cloud.tencent.com",@"adUrl":@"https://cloud.tencent.com/document/product",@"adTitile":@"This is a web display interface",@"adDes":@"This is a web display interface",@"name":@"@Mars",@"type":@"ad"};model1.extInfo = extr1;[modelArray insertObject:model1 atIndex:1];
///1、删除索引1处的数据和视图
[[self.videoView getDataManager] removeData:1];///2、添加一组数据到索引9处TUIPlayerVideoModel *model = [[TUIPlayerVideoModel alloc] init];model.viewType = TUI_ITEM_VIEW_TYPE_CUSTOM;[[self.videoView getDataManager] addData:model index:9];
参数名称 | 含义 |
removeData | 按索引移除数据 |
removeRangeData | 按范围移除数据 |
removeDataByIndex | 按索引数组移除数据 |
addData:index | 按索引添加数据 |
addRangeData:startIndex | 按模型数组从某个索引处添加数据 |
replaceData:index | 按索引替换数据 |
replaceRangeData:startIndex | 按模型数组从某个索引处替换数据 |
getDataByPageIndex | 读取某个索引处的数据 |
getCurrentDataCount | 获取当前播放列表内的数据总数 |
getCurrentIndex | 获取当前播放界面的数据索引 |
getCurrentModel | 获取当前播放界面的数据模型 |
TUIPlayerShortVideoUIManager *uiManager = [[TUIPlayerShortVideoUIManager alloc] init];[uiManager setControlViewClass: TUIPSControlView.class];[uiManager setLoadingView:[[TUIPSLoadingView alloc] init]];[uiManager setBackgroundView:[UIView new]];_videoView = [[TUIShortVideoView alloc] initWithUIManager:uiManager];
参数名称 | 含义 |
setLoadingView | 设置加载图 |
setBackgroundView | 设置背景图 |
setErrorView | 设置错误界面 |
setControlViewClass | 设置视频控制层* @param ViewClass 控制层类,ViewClass 是您封装好的视频控制 View,包含如进度条,时间 lable 等控件 * 它将被整体覆盖在视频窗口上,大小与视频窗口一致 |
setControlViewClass
:viewType | 设置不同类型的视频控制层 |
getLoadingView | 获取加载图实 View 实例 |
getBackgroundView | 获取背景图 View 实例 |
getErrorView | 获取错误界面 View 实例 |
getControlViewClass | 获取视频控制界面 View 类 |
getControlViewClassWithViewType | 获取不同类型视频控制界面类 |
参数说明 | 含义 |
delegate | 一个反向代理,用于控制层与播放层的交互 |
model | 当前播放的视频模型 |
currentPlayerStatus | 当前播放器的播放状态 |
showCenterView | 显示中心 view |
hideCenterView | 隐藏中心 view |
showLoadingView | 显示 loading 图 |
hiddenLoadingView | 隐藏 loading 图 |
setDurationTime | 总的视频时常 |
setCurrentTime | 当前的播放时长 |
setProgress | 进度条进度 |
showSlider | 显示进度条 |
hideSlider | 隐藏进度条 |
reloadControlData | 触发视图刷新 |
getPlayer | 获取播放器对象 |
onPlayEvent | 获取播放器事件 |
getVideoLayerRect | 获取视频渲染区域的变化 |
getVideoWidget | 获取视频渲染图层对象 |
参数说明 | 含义 |
delegate | 一个反向代理,用于控制层与播放层的交互 |
model | 当前播放数据模型 |
reloadControlData | 触发视图刷新 |
getPlayer | 获取播放器对象 |
getVideoLayerRect | 获取视频图层区域 |
getVideoWidget | 获取视频渲染图层 |
参数说明 | 含义 |
delegate | 一个反向代理,用于控制层与播放层的交互 |
model | 当前播放数据模型 |
reloadControlData | 触发视图刷新 |
类型 | 点播 | 直播 | Custom(轮播图) | Custom(图文广告) |
样例 | | | | |
@interface TUIPSControlView : UIView<TUIPlayerShortVideoControl>@property (nonatomic, strong) TUIPlayerVideoModel *videoModel@end@implementation TUIPSControlView-(instancetype)initWithFrame:(CGRect)frame {if ([super initWithFrame:frame]){/// UI布局代码}return self;}-(void)setModel:(TUIPlayerVideoModel *)model {_model = model;/// 数据}@end
@interface TUIPSControlLiveView : UIView<TUIPlayerShortVideoLiveControl>@property (nonatomic, strong) TUIPlayerLiveModel *videoModel@end@implementation TUIPSControlLiveView-(instancetype)initWithFrame:(CGRect)frame {if ([super initWithFrame:frame]){/// UI布局代码}return self;}-(void)setModel:(TUIPlayerLiveModel *)model {_model = model;/// 数据}@end
@interface TUIPSControlCustomView : UIView<TUIPlayerShortVideoCustomControl>@property (nonatomic, strong) TUIPlayerDataModel *videoModel@end@implementation TUIPSControlCustomView-(instancetype)initWithFrame:(CGRect)frame {if ([super initWithFrame:frame]){/// UI布局代码}return self;}-(void)setModel:(TUIPlayerDataModel *)model {_model = model;/// 数据NSDictionary *dic = model.extInfo;NSString *adTitile = [dic objectForKey:@"adTitile"];NSString *adDes = [dic objectForKey:@"adDes"];NSString *adUrl = [dic objectForKey:@"adUrl"];NSString *name = [dic objectForKey:@"name"];NSString *type = [dic objectForKey:@"type"];if ([type isEqualToString:@"ad"]) { ///图文广告self.webView.hidden = NO;self.cycleScrollView.hidden = YES;self.desLabel.textColor = [UIColor blackColor];self.nameLabel.textColor = [UIColor blackColor];[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:adUrl]]];} else if ([type isEqualToString:@"imageCycle"]) { ///轮播图self.webView.hidden = YES;self.cycleScrollView.hidden = NO;self.desLabel.textColor = [UIColor whiteColor];self.nameLabel.textColor = [UIColor whiteColor];NSString *imagesStr = [dic objectForKey:@"images"];NSArray *imagesArray = [imagesStr componentsSeparatedByString:@"<:>"];self.cycleScrollView.imageURLStringsGroup = imagesArray;}}@end
TUIPlayerShortVideoUIManager *uiManager = [[TUIPlayerShortVideoUIManager alloc] init];[uiManager setControlViewClass: TUIPSControlView.class viewType:TUI_ITEM_VIEW_TYPE_VOD];[uiManager setControlViewClass: TUIPSControlLiveView.class viewType:TUI_ITEM_VIEW_TYPE_LIVE];[uiManager setControlViewClass: TUIPSControlCustomView.class viewType:TUI_ITEM_VIEW_TYPE_CUSTOM];
_videoView = [[TUIShortVideoView alloc] initWithUIManager:uiManager];
// Set your playback strategyTUIPlayerVodStrategyModel *model = [[TUIPlayerVodStrategyModel alloc] init];model.mPreloadConcurrentCount = 1;model.preDownloadSize = 1;model.enableAutoBitrate = NO;[_videoView setShortVideoStrategyModel:model];// live strategyTUIPlayerLiveStrateyModel *liveStrateyModel = [[TUIPlayerLiveStrateyModel alloc] init];[_videoView setShortVideoLiveStrategyModel:liveStrateyModel];
类型 | 数据模型 | UI 模板 |
点播 | TUIPlayerVideoModel | TUIPSControlView |
直播 | TUIPlayerLiveModel | TUIPSControlLiveView |
自定义类型(轮播图,图文广告等) | TUIPlayerDataModel | TUIPSControlCustomView |
-(void)setModel:(TUIPlayerVideoModel *)model {if ([_model observationInfo]) {[_model removeObserver:self forKeyPath:@"preloadState"];}_model = model;[model addObserver:self forKeyPath:@"preloadState" options:NSKeyValueObservingOptionNew context:nil];NSDictionary *dic = model.extInfo;NSString *iconUrl = [dic objectForKey:@"iconUrl"];NSString *advertise = [dic objectForKey:@"advertise"];NSString *name = [dic objectForKey:@"name"];NSString *title = [dic objectForKey:@"title"];NSString *topic = [dic objectForKey:@"topic"];self.iconImageView.image = [UIImage imageNamed:iconUrl];[self.adButton setTitle:advertise forState:UIControlStateNormal];self.nameLabel.text= name;self.themeLabel.text = topic;self.desLabel.text = title;[self updatePreloadState];[self updateLickCount];model.onExtInfoChangedBlock = ^(id _Nonnull extInfo) {[self updateLickCount];};}
-(void)setModel:(TUIPlayerLiveModel *)model {_model = model;NSDictionary *dic = model.extInfo;NSString *adTitile = [dic objectForKey:@"liveTitile"];NSString *adDes = [dic objectForKey:@"liveDes"];NSString *name = [dic objectForKey:@"name"];self.nameLabel.text = name;self.desLabel.text = adTitile;}
-(void)setModel:(TUIPlayerDataModel *)model {_model = model;NSDictionary *dic = model.extInfo;NSString *adTitile = [dic objectForKey:@"adTitile"];NSString *adDes = [dic objectForKey:@"adDes"];NSString *adUrl = [dic objectForKey:@"adUrl"];NSString *name = [dic objectForKey:@"name"];NSString *type = [dic objectForKey:@"type"];self.desLabel.text = adTitile;self.nameLabel.text = name;if ([type isEqualToString:@"web"]) {self.webView.hidden = NO;self.cycleScrollView.hidden = YES;self.desLabel.textColor = [UIColor blackColor];self.nameLabel.textColor = [UIColor blackColor];[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:adUrl]]];} else if ([type isEqualToString:@"imageCycle"]) {self.webView.hidden = YES;self.cycleScrollView.hidden = NO;self.desLabel.textColor = [UIColor whiteColor];self.nameLabel.textColor = [UIColor whiteColor];NSString *imagesStr = [dic objectForKey:@"images"];NSArray *imagesArray = [imagesStr componentsSeparatedByString:@"<:>"];self.cycleScrollView.imageURLStringsGroup = imagesArray;}}
@protocol TUIPlayerShortVideoControlDelegate <NSObject>/*** 暂停*/- (void)pause;/*** 继续播放*/- (void)resume;/*** 滑动滚动条的处理* @param time 滑动的距离*/- (void)seekToTime:(float)time;/*** 是否正在播放*/- (BOOL)isPlaying;/*** 重置视频播放容器* - 用于视频播放容器被移除后需要重置的场景*/- (void)resetVideoWeigetContainer;@optional/*** 自定义回调事件*/- (void)customCallbackEvent:(id)info;@end/////调用if (self.delegate && [self.delegate respondsToSelector:@selector(pause)]) {[self.delegate pause];}
@protocol TUIPlayerShortVideoLiveControlDelegate <NSObject>/*** 暂停*/- (void)pause;/*** 继续播放*/- (void)resume;/*** 重置视频播放容器* - 用于视频播放容器被移除后需要重置的场景*/- (void)resetVideoWeigetContainer;@optional/*** 自定义回调事件*/- (void)customCallbackEvent:(id)info;@end/////调用if (self.delegate && [self.delegate respondsToSelector:@selector(pause)]) {[self.delegate pause];}
@protocol TUIPlayerShortVideoCustomControlDelegate <NSObject>@optional/*** 自定义回调事件*/- (void)customCallbackEvent:(id)info;@end/////调用if (self.delegate && [self.delegate respondsToSelector:@selector(customCallbackEvent:)]) {[self.delegate customCallbackEvent:@"test"];}
/// 获取数据管理器TUIShortVideoDataManager *dataManager = [self.videoView getDataManager];///获取数据模型TUIPlayerDataModel *model = [dataManager getDataByPageIndex:1];///修改数据模型model.extInfo = @{@"key":@"value"}///通知数据模型发生改变[model extInfoChangeNotify];
model.onExtInfoChangedBlock = ^(id _Nonnull extInfo) {[self updateLickCount];};
/// 音量均衡 .响度范围:-70~0(LUFS)。此配置需要LiteAVSDK 11.7 及以上版本支持。/// 以下几种常量供参考使用/// 关:AUDIO_NORMALIZATION_OFF (TXVodPlayConfig.h)/// 开(标准响度):AUDIO_NORMALIZATION_STANDARD (TXVodPlayConfig.h)/// 开(低响度):AUDIO_NORMALIZATION_LOW (TXVodPlayConfig.h)/// 开(高响度):AUDIO_NORMALIZATION_HIGH (TXVodPlayConfig.h)/// 默认值为AUDIO_NORMALIZATION_OFF。TUIPlayerVodStrategyModel *model = [[TUIPlayerVodStrategyModel alloc] init];model.audioNormalization = AUDIO_NORMALIZATION_STANDARD;[_videoView setShortVideoStrategyModel:model];
本页内容是否解决了您的问题?