更新时间:2023-02-02 14:07:44
云点播提供安卓上传SDK,可以嵌入到您的APP中,方便您将安卓移动设备中的文件上传至云点播。
有关详细信息,请访问获取视频上传令牌。
初始化设置上传域、超时、上传并发和重试。
com.chinanetcenter.wcs.android.Config.java
public static final String PUT_URL = "upload.cloudv.haplat.net";
FileUploader.setUploadUrl("upload.cloudv.haplat.net");
ClientConfig config = new ClientConfig();
// Set multi-part upload concurrency at 10,default at 5.
config.setMaxConcurrentRequest(10);
FileUploader.setClientConfig(config);
import com.chinanetcenter.wcs.android.api.ParamsConf;
conf = new ParamsConf();
conf.fileName = '<Name of the file to be uploaded>';
conf.keyName = <File name to be displayed on console>;
conf.mimeType = '<mimeType>';
FileUploader.setParams(conf);
默认块大小为 4M,必须是 4M 的倍数,最大不能超过 100M。默认块大小为 4M,必须是 64K 的倍数,并且不能超过块大小最大值。
FileUploader.setBlockConfigs(8, 512); //Set the block size at 8MB and the chunk size at 512KB
/**
* Example
*/
private void uploadFile(File srcFile) {
/**
* UPLOADER_TOKEN--upload token, which can be retrieved from Get Upload Token API.
* srcFile-content to be uploaded
*/
FileUploader.upload(UPLOADER_TOKEN, srcFile, new FileUploaderListener() {
/** Callback for upload success **/
@Override
public void onSuccess(int status, JSONObject responseJson) {
Log.d(TAG, "responseJson : " + responseJson.toString());
}
/** Callback for upload fail **/
@Override
public void onFailure(OperationMessage operationMessage) {
Log.e(TAG, "errorMessage : " + operationMessage.toString());
}
/** Callback for upload progress **/
@Override
public void onProgress(int bytesWritten, int totalSize) {
Log.d(TAG, String.format("Progress %d from %d (%s)", bytesWritten, totalSize, (totalSize > 0) ? ((float) bytesWritten / totalSize) * 100 : -1));
}
});
}
在移动设备上上传大文件需要很长时间,一旦文件上传出现异常终止,需要重新上传所有内容,影响用户体验。因此,我们建议您在上传大文件时使用 Multi-part Upload。
Multi-part Upload 是将一个大文件切割成多个块和自定义大小的块,然后将这些块并行上传。如果一个区块上传失败,客户端只需要重新上传这个区块即可。
private static final long DEFAULT_BLOCK_SIZE = 1 * 1024 * 1024;
/**
* context--relevant information
* uploadToken--upload token
* ipaFile--content to be uploaded
* DEFAULT_BLOCK_SIZE--block size
*/
FileUploader.sliceUpload(context, uploadToken, ipaFile, DEFAULT_BLOCK_SIZE, new SliceUploaderListener() {
/** Callback for upload success **/
@Override
public void onSliceUploadSucceed(JSONObject jsonObject) {
Log.d("CNCLog", "slice upload succeeded.");
}
/** Callback for upload fail **/
@Override
public void onSliceUploadFailured(OperationMessage operationMessage) {
Log.d("CNCLog", "slice upload failured.");
}
/** Callback for upload progress **/
@Override
public void onProgress(long uploaded, long total) {
Log.d("CNCLog", String.format(Locale.CHINA, "uploaded : %s, total : %s", uploaded, total));
}
});