本文为您详细介绍如何使用 PlayerKit 的进阶功能。
SceneKit 模块提供短剧场景控件,包含预加载和预渲染的完整使用方式。可前往 VESceneModule/ShortDrama 目录中查看源码。预加载和预渲染的详细介绍和实现原理请查看抖音同款短视频最佳实践。
注意
该功能仅高级版支持。请确保您已购买高级版的 License,详见播放器 License。
外挂字幕指与视频文件分离的字幕文件,用户可在播放时按需导入。PlayerKit 支持添加 WebVTT (Web Video Text Tracks) 和 SRT (SubRip Text) 格式的外挂字幕。外挂字幕的优势在于其灵活性,用户可按需选择是否加载字幕以及加载何种语言的字幕,且无需进行视频转码,只需在播放端设置即可显示。
注意
该功能仅高级版支持。请确保您已购买高级版的 License,详见播放器 License。
外挂字幕的实现流程具体如下:
开启字幕:
VEVideoPlayerConfiguration *configration = [VEVideoPlayerConfiguration defaultPlayerConfiguration]; // 字幕总开关 configration.enableSubtitle = YES; // 设置字幕源类型: // - VEPlayerKitSubtitleSourceAuthToken: Vid 字幕源 // - VEPlayerKitSubtitleSourceDirectUrl: DirectUrl 字幕源 configration.subtitleSourceType = VEPlayerKitSubtitleSourceAuthToken;
配置字幕 UI:
#import "VEPlayerSubtitleModule.h" @implementation ShortDramaDetailPlayerModuleLoader - (NSArray<id<VEPlayerBaseModuleProtocol>> *)getCoreModules { NSMutableArray *coreModules = [NSMutableArray array]; ... // 创建字幕模块 self.subtitleModule = [VEPlayerSubtitleModule new]; // 将字幕模块加入核心模块数组 [coreModules addObject:self.subtitleModule]; ... return coreModules; } // 更新字幕模块显示的文本 - (void)setSubtitle:(NSString *)subtitle { [self.subtitleModule setSubtitle:subtitle]; } @end self.moduleLoader = [[ShortDramaDetailPlayerModuleLoader alloc] init];
实现字幕委托:
#pragma mark ----- VEVideoPlayerControllerSubtitleDelegate // 字幕回调,此处可直接更新字幕模块显示的文本 - (void)videoPlayerController:(VEVideoPlayerController *)videoPlayerController onSubtitleTextUpdated:(NSString *)subtitle { dispatch_async(dispatch_get_main_queue(), ^{ [self.moduleLoader setSubtitle:subtitle]; }); } // 多语言字幕选择回调,此处需返回要使用的字幕对应的id - (NSInteger)getMatchedSubtitleId:(TTVideoEngineSubDecInfoModel *)subtitleInfoModel { return [VEDataManager getMatchedSubtitleId:subtitleInfoModel]; }
创建播放控件:创建 VEVideoPlayerController,使用指定的 configuration 和 moduleLoader,并指定 subtitleDelegate。
self.playerController = [[VEVideoPlayerController alloc] initWithConfiguration:configration moduleLoader:self.moduleLoader playerContainerView:self.view]; self.playerController.delegate = self; self.playerController.subtitleDelegate = self;
设置字幕信息:根据 configration.subtitleSourceType 配置,将 subtitleAuthToken 或者 subtitleModel 设置给 playerController。
// 使用 Vid 和 subtitleAuthToken 方式加载字幕 [self.playerController setSubtitleAuthToken:self.dramaVideoInfo.subtitleAuthToken]; // 使用 DirectUrl 方式加载字幕 [self.playerController setSubtitleInfoModel:subtitleInfoModel]; [self.playerController setSubtitleId:[VEDataManager getMatchedSubtitleId:subtitleInfoModel]];
字幕预加载:
// 如果开启了字幕预加载,并且使用 DirectUrl 视频源,则在设置预加载信息时,一并设置字幕预加载信息 // 注意:目前仅 DirectUrl 视频源支持支持字幕预加载 NSMutableArray *sources = [NSMutableArray array]; loop { TTVideoEngineUrlSource *urlSource = [[TTVideoEngineUrlSource alloc] initWithUrl:url cacheKey:url.btd_md5String videoId:vid]; TTVideoEngineSubDecInfoModel *subtitleInfoModel = [[TTVideoEngineSubDecInfoModel alloc] initWithDictionary:subtitleInfoDict]; urlSource.subtitleId = [VEDataManager getMatchedSubtitleId:subtitleInfoModel]; urlSource.subtitleInfoModel = subtitleInfoModel; [sources addObject:urlSource]; } [VEVideoPlayerController setStrategyVideoSources:sources];
若在开启字幕的同时开启了预渲染功能,则需在设置策略之前设置预渲染字幕信息:
- (void)setPrerenderSubtitleModels { if ([[VESettingManager universalManager] settingForKey:VESettingKeyShortVideoPreRenderStrategy].open) { NSDictionary *subtitleModels = [VEDramaDataManager buildSubtitleModels:self.dramaVideoModels]; if (subtitleModels) { [VEPreRenderVideoEngineMediatorDelegate shareInstance].subtitleModels = subtitleModels; } } }