直播带货平台开发怎样实现ios端的视频云存储
直播带货平台开发中,视频存储是重要的一环,而视频存储离不开服务器的支持。因为物理服务器运维成本比较高,很多直播带货平台开发部署中,使用的都是云服务器。我们以腾讯云为例,介绍一下在进行ios端直播带货平台开发时,如何实现腾讯云存储以及视频播放。
一、视频存储
1.进行直播带货平台开发首时,先下载三方库添加到工程,拷贝上传源码目录TXUGCUploadDemo/upload到您的工程中。
2.导入动态库QCloudCore.framework、QCloudCOSXML.framework和静态库libmtasdk.a(在TXUGCUploadDemo/upload/COSSDK/目录下),到您的工程中,并添加以下依赖库:
(1) CoreTelephony.framework (2) Foundation.framework (3) SystemConfiguration.framework (4) libc++.tbd
3.请求接口获取上传信息appid:
YBWeakSelf; NSDictionary *parDic = @{@"uid":[Config getOwnID],@"token":[Config getOwnToken]}; [YBNetworking postWithUrl:@"Video.getTxVodSignature" Dic:parDic Suc:^(int code, id info, NSString *msg) { if (code == 0) { NSDictionary *infos = [info firstObject]; [weakSelf uploadWithTXDic:infos]; } } Fail:^(id fail) { }];
4.初始化上传对象,设置上传回调,在TXPublishParam里添加数据参数,由业务后台产生的签名、视频文件路径,开始上传
if (_videoPublish == nil) { _videoPublish = [[TXUGCPublish alloc] initWithUserID:minstr([txDic valueForKey:@"appid"])]; _videoPublish.delegate = self; } TXPublishParam *publishParam = [[TXPublishParam alloc] init]; publishParam.signature = minstr([txDic valueForKey:@"signature"]); publishParam.videoPath = _originalVdieoPath; [_videoPublish publishVideo:publishParam]; #pragma mark - TXVideoPublishListener - (void)onPublishProgress:(NSInteger)uploadBytes totalBytes:(NSInteger)totalBytes { NSLog(@"onPublishProgress [%ld/%ld]", uploadBytes, totalBytes); _allUploadPro = (_originalCoverPro+_originalVideoPro+_mkVideoPro)/_uploadNum; _allUploadPro = uploadBytes/totalBytes; NSLog(@"allll:%f",_allUploadPro); [[NSNotificationCenter defaultCenter]postNotificationName:@"uploadProNot" object:@(_allUploadPro)]; } - (void)onPublishComplete:(TXPublishResult*)result { YBWeakSelf; NSString *string = [NSString stringWithFormat:@"上传完成,错误码[%d],信息[%@]", result.retCode, result.retCode == 0? result.videoURL: result.descMsg]; if (result.retCode == 0) { txVideoid = result.videoId; [weakSelf requstAPPServceTitle:_videoTitle andVideo:result.videoURL andImage:_cosFinishCoverPath andWaterName:_cosFinishMKVideoPath]; } NSLog(@"onPublishComplete [%d/%@]", result.retCode, result.retCode == 0? result.videoURL: result.descMsg); }
上传成功获取返回的videoID上传服务器,自此整个上传视频过程完成,直播带货平台开发实现视频存储后,又是怎样实现视频播放的呢?
二、视频播放
1.首先根据视频ID获取视频的详细信息
-(void)getvideoDetail:(id)infoDic andCode:(int)code msgStr:(NSString *)msg{ NSDictionary *_currentVideoDic =[infoDic firstObject]; NSString *videoId = [_currentVideoDic valueForKey:@"videoid"]; NSString *vodeoUrl = [NSString stringWithFormat:@"http://www.cecepgc.cn:81/getTxMediaInfo?videoid=%@",videoId]; YBWeakSelf; [iOSNetworking getWithURL:vodeoUrl params:nil success:^(id responseObject) { NSDictionary *data = responseObject; if (![data isEqual:[NSNull null]]) { NSDictionary *playInfo = [[[data valueForKey:@"MediaInfoSet"]firstObject] valueForKey:@"TranscodeInfo"]; NSString *playurl = minstr([[[playInfo valueForKey:@"TranscodeSet"]firstObject] valueForKey:@"Url"]); NSString *keyurl = [PublicObj getVideoChainWithUrl:playurl]; self.player.assetURL =[NSURL URLWithString:keyurl] ; RKAliPlayerManager *manager = (RKAliPlayerManager*)self.player.currentPlayerManager; [manager aliplay]; //video_play_type }
2.获取到视频详细信息,取出视频链接进行防盗链的处理,在URL尾部以 QueryString 的方式加入防盗链参数
步骤1:确定防盗链参数
参数名 | 取值 | 说明 |
KEY | 24FEQmTzro4V5u3D5epW | 开发者开通 Key 防盗链时选择的密钥 |
Dir | /dir1/dir2/ | 原始播放 URL 的 PATH 中除去myVideo.mp4的剩余部分 |
t | 5a71afc0 | 过期时间戳1517400000的十六进制表示结果 |
us | 72d4cd1101 | 生成的随机字符串 |
步骤2:计算签名
sign=md5("24FEQmTzro4V5u3D5epW/dir1/dir2/5a71afc072d4cd1101") = "3d8488faeb37d52d6bf63b63c1b171c3"
步骤3:生成防盗链 URL
将防盗链参数拼接到视频原始 URL 的 QueryString 中,得到视频防盗链 URL:
http://example.vod2.myqcloud.com/dir1/dir2/myVideo.mp4?t=5a71afc0&us=72d4cd1101&sign=3d8488faeb37d52d6bf63b63c1b171c3
3.获取到防盗链地址就可以播放了,具体实现如下:
+(NSString *)getVideoChainWithUrl:(NSString *)urlStr{ NSString *key = [self decrypt:[common getTxvodKey]]; if (key.length < 1) { return urlStr; } NSArray *arr1 = [urlStr componentsSeparatedByString:@"://"]; NSString *urls; if (arr1.count > 0) { urls = arr1[1]; } NSArray *arr2 = [urls componentsSeparatedByString:@"/"]; NSString *dirStr; if (arr2.count > 0) { dirStr = [NSString stringWithFormat:@"/%@/%@/",arr2[1],arr2[2]]; } long time; NSDate *fromdate=[NSDate date]; time=(long)[fromdate timeIntervalSince1970]; time = time +[[common getTxvodKeyLength]intValue]; NSString *videoUrl = [NSString stringWithFormat:@"%@%@%ld20210902",key,dirStr,time]; videoUrl = [self stringToMD5:videoUrl]; NSString *newVideoUrl = [NSString stringWithFormat:@"%@?t=%ld&us=%@&sign=%@",urlStr,time,@"20210902",videoUrl]; return newVideoUrl;
就这样,直播带货平台开发就完成了视频的存储和播放。
声明:以上内容为云豹科技原创,未经作者本人同意,禁止转载,否则将追究相关法律责任www.yunbaokj.com