You need to enable JavaScript to run this app.
导航
快速开始
最近更新时间:2025.04.14 19:05:26首次发布时间:2025.04.14 19:05:26
我的收藏
有用
有用
无用
无用

PlayerKit 是火山引擎推出的开源 iOS 播放器 UI 组件,封装播放器 SDK 核心功能并内置标准化 UI 组件,集成播放控制、多清晰度切换、小窗播放、播放质量监控、视频加密及极速高清等能力。该组件提供高可扩展的播放器架构设计和抖音同款播放策略实现,支持通过标准化 API 快速构建符合主流视频应用体验的播放功能,适配短视频、长视频、点播等多种业务场景。

准备工作

视频点播通过 License 管理播放器 SDK 及其增值服务的使用权限和时间。在项目测试阶段,您可以在视频点播控制台申请免费的测试 License。您需完成以下操作:

注意

测试 License 仅限项目测试阶段使用,不可续期。测试 License 到期会导致鉴权失败,进而影响业务的正常使用。项目上线前,请升级至正式 License

集成 PlayerKit

步骤 1:下载项目

PlayerKit 的项目地址是 PlayerKit_iOS。执行以下 Git 命令下载项目工程:

git clone https://github.com/volcengine/VEVodDemo-iOS
cd VOLCDemo

步骤 2:拷贝项目

  1. 将以下文件夹复制到您的项目文件夹中,确保其与 podfile 文件处于同一目录层级。

    # 拷贝以下文件夹到项目中
    VEBaseKit
    VEPlayerKit
    VEPlayerUIModule
    
  2. 替换播放器资源文件:VEVodMain/Resources 文件夹用于存放播放器的资源文件。在开发过程中,可直接将 VEVodMain/Resources 文件夹复制到工程里,再依据业务需求替换或新增资源文件。需注意,上线前要检查并删除无用的资源文件,以减小安装包体积。

说明

复制操作完成后,建议执行一次 git commit 命令,并在提交信息中记录 VEVodDemo-iOS 当前的最新提交 ID。源代码可能会发生变动,本次提交有助于您进行追溯。

步骤 3:配置 CocoaPods

打开项目的 Podfile 文件,参考以下代码修改 Pod 依赖。完成修改后,执行 pod install --repo-update 命令安装 Pod 依赖。

source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/volcengine/volcengine-specs.git'

platform :ios, '11.0'

target 'VOLCDemo' do
  
  # 这里需要明确指定使用 subspecs => Player-SR
  # 可在 https://www.volcengine.com/docs/4/66438 获取版本号,推荐使用最新版本
  pod 'TTSDKFramework', 'x.x.x.x-premium', :subspecs => ['Player-SR']
  
  # 依赖的三方开源库,一般项目中也会使用到这两个常用的开源库,开源库的版本以业务为准
  pod 'Masonry'
  pod 'SDWebImage'
  pod 'Reachability'
  pod 'MBProgressHUD', '~> 1.2.0'

end

步骤 4:添加 License 文件

请将申请到的 License 文件添加到项目工程目录下。完成以上四个步骤得到工程目录如下:
Image

开始播放

步骤 1:初始化

  1. AppDelegate 文件里参考以下示例代码初始化 PlayerKit:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        // app 启动初始化 TTSDK
        [self initTTSDK];
        
        return YES;
    }
    
    - (void)initTTSDK {
        // Debug日志,建议Debug期间打开
    #ifdef DEBUG
        [TTVideoEngine setLogFlag:TTVideoEngineLogFlagAll];
    #endif
        
        /// App 基本信息配置
        NSString *appId = @"替换为您在火山申请的 AppId";
        NSString *licenseName = @"替换为申请的 License 文件名"; // 例如 “ttsdkdemo.license”
    
        /// 配置 Lisence
        TTSDKConfiguration *configuration = [TTSDKConfiguration defaultConfigurationWithAppID:appId licenseName:licenseName];
    
        // 设置最大缓存 Size,默认 100M,可根据自身业务场景调整,超过缓存大小按照 LRU 规则清理
        TTSDKVodConfiguration *vodConfig = [[TTSDKVodConfiguration alloc] init];
        vodConfig.cacheMaxSize = 300 * 1024 *1024; // 建议设置大小 300M
        configuration.vodConfiguration = vodConfig;
    
        [TTSDKManager startWithConfiguration:configuration];
    }
    

步骤 2:创建 ModuleLoader

参考以下创建一个包含基本 UI 控件(播放按钮和进度条)的 ModuleLoader:

#import "SimplePlayerKitPlayerModuleLoader.h"
#import "VEPlayerLoadingModule.h"
#import "VEPlayerSeekModule.h"
#import "VEPlayerSeekProgressModule.h"
#import "VEPlayerPlayModule.h"

@interface SimplePlayerKitPlayerModuleLoader ()

@end

@implementation SimplePlayerKitPlayerModuleLoader

- (NSArray<id<VEPlayerBaseModuleProtocol>> *)getCoreModules {
    NSMutableArray *coreModules = [NSMutableArray array];
    [coreModules addObject:[VEPlayerLoadingModule new]];
    [coreModules addObject:[VEPlayerSeekModule new]];
    [coreModules addObject:[VEPlayerSeekProgressModule new]];
    [coreModules addObject:[VEPlayerPlayModule new]];
    return coreModules;
}

@end

步骤 3:设置播放源

参考以下示例代码直接使用视频 URL 进行播放。您需要将示例代码中的 URL 替换成真实的视频播放地址。

#import "SimplePlayerKitSceneViewController.h"
#import "VEVideoPlayerController.h"
#import "SimplePlayerKitPlayerModuleLoader.h"
#import <Masonry/Masonry.h>
#import "NSString+BTDAdditions.h"

@interface SimplePlayerKitSceneViewController ()

@property (nonatomic, strong) VEVideoPlayerController *playerController;
@property (nonatomic, strong) SimplePlayerKitPlayerModuleLoader *moduleLoader;

@end

@implementation SimplePlayerKitSceneViewController

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    // 退出页面关闭播放器
    [self close];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 1. 初始化播放器实例
    /// 初始化播放器配置
    VEVideoPlayerConfiguration *playerConfiguration = [VEVideoPlayerConfiguration defaultPlayerConfiguration];
    /// 初始化 modules loader
    _moduleLoader = [[SimplePlayerKitPlayerModuleLoader alloc] init];
    /// 初始化播放器
    _playerController = [[VEVideoPlayerController alloc] initWithConfiguration:playerConfiguration moduleLoader:_moduleLoader];
    
    // 2. 添加播放视图到显示视图
    [self.view addSubview:self.playerController.view];
    [self.playerController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).with.offset(-84);
    }];
    
    // 3. 设置播放源
    id<TTVideoEngineMediaSource> urlSource = [self createUrlMediaSource];
    [self.playerController setMediaSource:urlSource];
    
    // 4. 开始播放
    [self play];
}

- (void)play {
    [self.playerController play];
}

- (void)pause {
    [self.playerController pause];
}

- (void)close {
    [self.playerController close];
}

- (id<TTVideoEngineMediaSource>)createUrlMediaSource  {
    NSString *url = @"your play url";
    NSString *cacheKey = [url btd_md5String];
    TTVideoEngineUrlSource *urlSrouce = [[TTVideoEngineUrlSource alloc] initWithUrl:url cacheKey:cacheKey];
    return urlSrouce;
}

- (id<TTVideoEngineMediaSource>)createVidMediaSource  {
    NSString *vid = @"your video id";
    NSString *playAuthToken = @"your video id's playAuthToken";
    TTVideoEngineResolutionType resolution = TTVideoEngineResolutionTypeFullHD; // default resolution
    TTVideoEngineVidSource *vidSource = [[TTVideoEngineVidSource alloc] initWithVid:vid playAuthToken:playAuthToken resolution:resolution];
    return vidSource;
}

@end

后续操作

至此,您已实现基础的播放功能。后续可参考以下文档了解 PlayerKit 的核心类以及进阶功能: