有奖捉虫:行业应用 & 管理与支持文档专题 HOT
本文主要介绍如何在 iOS 端播放 HLS 私有加密视频。


前置步骤

创建加密视频搭建 token 服务,详情请参见 HLS 视频加密播概述


iOS 端播放指引

注意:
配置支持 HTTP 请求,需要在项目的 info.plist 文件中添加 App Transport Security Settings->Allow Arbitrary Loads设置为 YES。
iOS 端使用 CIPlayerAssistor SDK 中封装好的 CIPlayerAssistor 和 CIMediaConfig 对象来播放 m3u8 文件,用户按照如下规则传入参数,即可实现播放功能。
1. 集成 CIPlayerAssistor SDK
podfile 文件中新增:
pod 'CIPlayerAssistor', :git => 'https://github.com/tencentyun/CIPlayerAssistor_iOS.git'
执行pod install即可。
导入头文件:
#import <CIPlayerAssistor/CIPlayerAssistor.h>

2. 构造 CIMediaConfig 对象,参数说明如下:
参数名
说明
是否必填
类型
默认值
fileUrl
请求m3u8接口的文件地址
NSString
CIPlayerAssistor 参数说明如下:
参数名
说明
是否必填
类型
默认值
config
媒体文件配置信息
CIMediaConfig
token
token
NSString
signature
授权签名信息
NSString
3. iOS 端完整示例代码
@property (strong, nonatomic)AVPlayer *myPlayer;//播放器
@property (strong, nonatomic)AVPlayerItem *item;//播放单元
@property (strong, nonatomic)AVPlayerLayer *playerLayer;//播放界面(layer)

// 创建媒体配置对象
// fileUrl:文件链接。
CIMediaConfig * config = [[CIMediaConfig alloc]initWithFileUrl:@"https://ci-h5-bj-1258125638.cos.ap-beijing.myqcloud.com/hls/BigBuckBunny.m3u8?ci-process=pm3u8"];

// CIMediaConfig 类在实例化时 自动生成了公钥 config.publicKey,可用于请求token;
[self getToken:config.publicKey fileURL:config.fileUrl protectContentKey:1 callBack:^(NSString * _Nonnull token, NSString * _Nonnull signature) {
dispatch_async(dispatch_get_main_queue(), ^{
// 设置授权信息,会在url上通过&拼接传入的signature
// 如果原始url是cdn的话,不用传cos的signature
[[CIPlayerAssistor singleAssistor] buildPlayerUrlWithConfig:config withToken:token withSignature:signature buildUrlcallBack:^(NSString * _Nullable url, NSError * _Nullable error) {
AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:url]];
self.myPlayer = [AVPlayer playerWithPlayerItem:item];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.myPlayer];
self.playerLayer.frame = CGRectMake(0, 100, self.view.bounds.size.width, 300);
[self.view.layer addSublayer:self.playerLayer];
[self.myPlayer play];
}];
});
}];

- (void)getToken:(NSString *)publickey fileURL:(NSString *)fileURL protectContentKey:(int)protect callBack:(void (^)(NSString * token ,NSString * signature))callBack{
// 该url仅为示例,请替换成您业务的url,具体实现请参考 “业务后端示例代码”
NSMutableURLRequest * mrequest = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://cos.cloud.tencent.com/samples/hls/token"]];
mrequest.HTTPMethod = @"POST";
[mrequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSData *data = [publickey dataUsingEncoding:NSUTF8StringEncoding];
publickey = [data base64EncodedStringWithOptions:0];
NSDictionary * body = @{
@"src":fileURL,
@"publicKey":publickey,
@"protectContentKey":@(protect)
};
mrequest.HTTPBody = [NSJSONSerialization dataWithJSONObject:body options:NSJSONWritingFragmentsAllowed error:nil];
[[[NSURLSession sharedSession]dataTaskWithRequest:mrequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary * result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
callBack(result[@"token"],result[@"authorization"]);

}]resume];

}


Demo 体验

我们提供了终端播放 HLS 私有加密视频的示例,请参考 CIPlayerAssistor Demo