Skip to content
zanxiaofei edited this page Aug 29, 2017 · 1 revision

视频上传

SDK对ks3上传做了基本封装,您也可以参考ks3官网自己处理

1. 准备上传内容

KS3ClientWrap.KS3UploadInfo 通过该上传数据类,准备上传必要信息

               String mineType = FileUtils.getMimeType(new File(mLocalPath));
                StringBuilder objectKey = new StringBuilder(getPackageName() +
                        "/" + System.currentTimeMillis());
                if (mineType == FileUtils.MINE_TYPE_MP4) {
                    objectKey.append(".mp4");
                } else if (mineType == FileUtils.MINE_TYPE_GIF) {
                    objectKey.append(".gif");
                }
                mCurObjectKey = objectKey.toString();
                //上传必要信息:bucket(ks3项目相关bucket),objectkey(文件名),及PutObjectResponseHandler(上传过程回调)
                KS3ClientWrap.KS3UploadInfo bucketInfo = new KS3ClientWrap.KS3UploadInfo
                        ("ksvsdemo", mCurObjectKey, mPutObjectResponseHandler);

2. 触发上传

调用KS3ClientWrap的putObject接口来触发上传,并在KS3ClientWrap.OnGetAuthInfoListener的回调中完成上传的鉴权

   /**
     * upload file to KS3 Server
     *
     * @param bucketInfo               the params using Ks3Client
     * @param path                     the file path which need to upload
     * @param putObjectResponseHandler upload process callback  //上传过程回调
     * @param onGetAuthInfoListener    get Ks3's token callback
     */
    public void putObject(KS3UploadInfo bucketInfo, String path,
                          PutObjectResponseHandler putObjectResponseHandler,
                          OnGetAuthInfoListener onGetAuthInfoListener) 

示例代码:

 //调用SDK内部接口触发上传
               //mEditKit.getKS3ClientWrap()获取ks3上传工具类实例
               //mLocalPath 上传文件的本地路径  
               //上传鉴权回调
                mEditKit.getKS3ClientWrap().putObject(bucketInfo, mLocalPath,
                        mPutObjectResponseHandler, new KS3ClientWrap.OnGetAuthInfoListener() {
                            @Override
                            public KS3ClientWrap.KS3AuthInfo onGetAuthInfo(String s, String s1, String s2, String s3, String s4, String s5) {

                                if (mTokenTask == null) {
                                    mTokenTask = new KS3TokenTask(getApplicationContext());
                                }
                                //获取鉴权token
                                KS3ClientWrap.KS3AuthInfo authInfo = mTokenTask.requsetTokenToAppServer(s, s1,
                                        s2, s3, s4, s5);

                                return authInfo;
                            }
                        });

KS3TokenTask鉴权的AppServer地址为demo使用的临时地址,不可用于上线,服务器实现参考server

3. 上传过程回调

KS3Client提供PutObjectResponseHandler回调接口,返回上传过程中信息

示例代码

private PutObjectResponseHandler mPutObjectResponseHandler = new PutObjectResponseHandler() {
       @Override
        public void onTaskFailure(int statesCode, Ks3Error error, Header[] responceHeaders, String response, Throwable paramThrowable) {
            Log.e(TAG, "onTaskFailure:" + statesCode);
            onUploadFinished(false);
        }

        @Override
        public void onTaskSuccess(int statesCode, Header[] responceHeaders) {
            Log.d(TAG, "onTaskSuccess:" + statesCode);
            onUploadFinished(true);
        }

        @Override
        public void onTaskStart() {
            Log.d(TAG, "onTaskStart");
            onUploadStart();
        }

        @Override
        public void onTaskFinish() {
            Log.d(TAG, "onTaskFinish");
            mUploadState.set(UPLOAD_STATE_NONE);
        }

        @Override
        public void onTaskCancel() {
            Log.d(TAG, "onTaskCancel");
            mUploadState.set(UPLOAD_STATE_NONE);
        }

        @Override
        public void onTaskProgress(double progress) {
            onUploadProgress(progress);
        }
    };

4. 取消上传

上传过程中,可调用cancel()接口取消上传

            //取消上传,直接预览播放本地视频
            mKS3Wrap.cancel();
            onUploadFinished(false);

5. 编辑合成后视频自动上传

您可在回调KSYEditKit的OnInfoListener的回调状态ShortVideoConstants.SHORTVIDEO_COMPOSE_FINISHED中,自动触发视频上传,只需要返回上传需要的数据信息KS3ClientWrap.KS3UploadInfo即可
示例代码:

 private KSYEditKit.OnInfoListener mOnInfoListener = new KSYEditKit.OnInfoListener() {
        @Override
        public Object onInfo(int type, String... msgs) {
            Log.d(TAG, "on info:" + type);
            switch (type) {
                case ShortVideoConstants.SHORTVIDEO_COMPOSE_FINISHED: {
                    // 可在此处触发sdk将合成后文件上传到ks3,示例代码如下:
                    //上传必要信息:bucket,objectkey,及PutObjectResponseHandler上传过程回调
                    String mineType = FileUtils.getMimeType(new File(msgs[0]));
                    StringBuilder objectKey = new StringBuilder(getPackageName() +
                            "/" + System.currentTimeMillis());
                    if (mineType == FileUtils.MINE_TYPE_MP4) {
                        objectKey.append(".mp4");
                    } else if (mineType == FileUtils.MINE_TYPE_GIF) {
                        objectKey.append(".gif");
                    }
                    mCurObjectKey = objectKey.toString();
                    KS3ClientWrap.KS3UploadInfo bucketInfo = new KS3ClientWrap.KS3UploadInfo
                            ("ksvsdemo", mCurObjectKey, mPutObjectResponseHandler);
                    return bucketInfo;
                ......
             }
          }
     }
Clone this wiki locally