阿里云点播SDK集成(一)上传篇
目前项目中用到的就是阿里云的点播包括视频上传及播放,有做短视频的宝子们可以借鉴一下,避免少走弯路
1.省略开通或配置阿里云点播服务的平台配置,直接是iOS移动端部分,pod SDK 尽量用最新的
#阿里云播放器 pod 'AliPlayerSDK_iOS' #阿里云文件上传 pod 'VODUpload'
2.获取STS服务,所谓的STS就是获取对应的参数(accessKeyId、accessKeySecret、securityToken、expiration),这些服务存在自己后台(找自己后台去联调)
3.先说说上传,上传主要是通过阿里云的上传api进行实现的,特别要注意的一点就是我这边用的是swift的方式进行调用的,所以init的时候需要用`init`的方式调用
/** STS授权方式初始化 @deprecated 使用`setKeyId: accessKeySecret: secretToken: expireTime: listener:`方法 */ - (BOOL) init:(NSString *)accessKeyId accessKeySecret:(NSString *)accessKeySecret secretToken:(NSString *)secretToken expireTime:(NSString *)expireTime listener:(VODUploadListener *) listener __attribute__((deprecated("This method is conflict with swift initialization method, use `setKeyId: accessKeySecret: secretToken: expireTime: listener:` instead.")));
下面还是用我的实际操作来说明:(注:1.上面说过在调用阿里云上传的时候init需要加引号`init`; 2.listener监听的这4个也需要写完整finish、failure、started、progress; 3.listener监听的回调都是在异步上回调的,如需渲染UI请切换到主线程; 4.上传需要传入文件的file,文章最后补充下这里)
class AliUploadFileManager: NSObject { //单例 static let shared = AliUploadFileManager() //上传client var client = VODUploadClient() //监听listener let listener = VODUploadListener() //MARK: 上传文件 public func uploadFileToOSS(filePath: String, complete: @escaping(String?, String?) -> Void, uploadResponse: @escaping(Bool) -> Void) {
//这里根据业务情况填写或看阿里云文档 let vodInfo = VodInfo() vodInfo.title = "video_upload_after_name" vodInfo.desc = "video_upload_after_desc" vodInfo.cateId = 0 vodInfo.tags = "video_upload_after_tags" let path = filePath.replacingOccurrences(of: "file://", with: "") let uploadCertificate = VVSTSManager.shared let _ = client.`init`(uploadCertificate.accessKeyId, accessKeySecret: uploadCertificate.accessKeySecret, secretToken: uploadCertificate.securityToken, expireTime: uploadCertificate.expiration, listener: listener) //这里的workflowId和region也是平台配置的 client.workflowId = "" client.region = "" client.addFile(path, vodInfo: vodInfo) client.start() //监听上传完成 listener.finish = { (fileInfo, result) in print("上传完成", result?.imageUrl ?? "") DispatchQueue.main.async { complete(result?.videoId, result?.imageUrl) uploadResponse(true) } } //监听上传失败 listener.failure = { (fileInfo, code, message) in DispatchQueue.main.async { print("上传失败", code ?? "", message ?? "") uploadResponse(false) } } listener.started = { fileInfo in print("开始上传") } listener.progress = { fileInfo, upSize, totalSize in print("上传进度", upSize, totalSize) } } }
以上仅为借鉴,用oc的可以自己试着转换下,然后以下是调用部分:
//视频路径 AliUploadFileManager.shared.uploadFileToOSS(filePath: path) { videoId, videoURL in //videoId 上传完成后的文件id;videoURL上传完成后的文件网络地址 }
回调可以拿到上传后文件的id和文件的网络地址(根据listener.finish的回调看完整的地址是否需要拼接, 图片亦是如此)
补充:
无论是第三方拍照或是从相册里面选择相片,都可以通过以下的方式获得图片、视频的本地URL Path,如果不能那你自己想办法,我自己用的是ZLPhotoBrowser简称ps,用其他工具的需要拿到asset后利用此原理获取URL Path
ps.selectImageBlock = { [weak self] (results, isOriginal) in guard let `self` = self else { return } if let result = results.first { if result.asset.mediaType == .video { ToastTool.showLoadingHUD("视频上传中……") let options = PHVideoRequestOptions() options.version = PHVideoRequestOptionsVersion.current options.deliveryMode = PHVideoRequestOptionsDeliveryMode.automatic PHImageManager.default().requestAVAsset(forVideo: result.asset, options: options) { asset, audioMix, info in if let urlAsset = asset as? AVURLAsset { let path = urlAsset.url.absoluteString //视频路径 AliUploadFileManager.shared.uploadFileToOSS(filePath: path) { videoId, videoURL in // } } } } else { let resources = PHAssetResource.assetResources(for: result.asset) if let resource = resources.first, let pathURL = resource.value(forKey: "privateFileURL") as? URL { //图片路径 ToastTool.showLoadingHUD("图片上传中……") let path = pathURL.absoluteString AliUploadFileManager.shared.uploadFileToOSS(filePath: path) { imageId, imageURL in resultBlock(result.image, nil, imageURL) } } } } }
至此,上传的地方就完成可用于视频或图片的上传,赶进度的宝子们直接复制,有问题在下方评论,看到后第一时间答复