快速开始
更新时间:2024-12-27
初始化 SDK
string mediaURL = "http://XXX:8191/medias/upload"; //申请上传服务接口URL
string completeURL = "http://XXX:8191/medias/upload_done"; //确认上传服务接口地址
VodUpload VodUpload = new VodUpload(mediaURL,completeURL);
说明:
1、申请上传服务接口URL、确认上传服务接口地址为请求VOD服务时的接口地址,具体请参考API文档。
demo示例
引入sdk
import com.baidubce.uploader.UploadStatus;
import com.baidubce.uploader.UploaderListener;
import com.baidubce.uploader.VodUpload;
import com.baidubce.uploader.UploaderInfo;
简单示例
new Thread() {
@Override
public void run() {
try {
VodUpload VodUpload = new VodUpload("申请上传服务接口URL","确认上传服务接口地址");
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException("The media file " + file.getAbsolutePath() + " doesn't exist!");
}
String sourceExtension = null;
String filename = file.getName();
sourceExtension = getFileExtension(filename);
VodUpload.setListener(new UploaderListener() {
@Override
public void onPublishEvent(UploaderInfo info) {
Log.d("uploader", "upload file onPublishReady status is " + info.status+"id is "+info.id + "进度"+info.percent);
}
@Override
public void onPublishProgress(UploaderInfo info) {
Log.d("uploader", "upload file onPublishComplete status is " + info.status+"id is "+info.id + "进度"+info.percent);
}
@Override
public void onPublishComplete(UploaderInfo info) {
Log.d("uploader", "upload file onPublishComplete status is " + info.status+"id is "+info.id + "进度"+info.percent+mediaId is"+info.mediaId);
}
@Override
public void onError(UploaderInfo info) {
Log.d("uploader", "上传失败,error is"+info.errMsg);
}
});
UploaderInfo UploaderInfo = VodUpload.upload(file);
;} catch (Throwable e) {
e.printStackTrace();
// Exception means status failed
toastMessage(true, "上传失败,错误信息:" + e.getMessage());
}
}
}.start();
高级功能
上传文件
VodUpload.upload(file)
说明:
1、file为待上传的文件,支持上传文件类型:.mp4, .flv, .mov, .3gp, .avi, .mpg, .asf, .wmv, .mkv, .webm, .mxf, .mp3, .m4a, .flac, .ogg, .wav, .ra, .aac, .amr等音视频文件。
暂停上传
SDK支持通过stop()方法,暂停正在上传的文件
VodUpload.stop();
恢复上传
已经暂停的文件,可以通过resume()方法恢复上传
VodUpload.resume()
取消上传
SDK支持通过cancel()方法,取消正在上传的文件
VodUpload.cancel()
说明:
1、取消上传的文件,结束并上传实例销毁(不可恢复)
设置是否分片上传
SDK支持通过setMultipartUpload(boolean val)方法设置是否开启分片上传,默认为开启
VodUpload.setMultipartUpload(false)
设置分片大小
SDK支持通过setSliceSize(long size)方法设置分片大小。单位为 byte,默认值为 16 1024 1024。
VodUpload.setSliceSize(8 * 1024 * 1024)
开启并行上传的线程数
SDK支持通过 setSocketNum(int num)方法设置上传线程数。默认值为 1。
VodUpload.setSocketNum(2)
设置是否自动提取封面
SDK支持通过 setAutoExtractCover(boolean val)方法设置是否自动提取封面。默认为false。
VodUpload.setAutoExtractCover(true)
获取上传信息
UploaderInfo info = VodUpload.getUploaderInfo()
说明:
1、UploaderInfo参数信息见接口描述-UploaderInf】。
断点续传
断点续传功能的前提是需要分片上传,SDK 默认开启了分片上传和断点续传功能,无需额外操作。但断点续传功能仅限于为退出app,如果退出app则信息将会被清除。
应用场景:上传文件暂停后,如果恢复上传可以从可以从中断处继续上传,减少重复上传时间。
事件监听
VodUpload.setListener(new UploaderListener() {
@Override
public void onPublishEvent(UploaderInfo info) {
Log.d("uploader", "upload file onPublishReady status is " + info.status+"id is "+info.id + "进度"+info.percent);
}
@Override
public void onPublishProgress(UploaderInfo info) {
tvFileSelected.setText("上传进度:" + info.percent + "%");
Log.d("uploader", "upload file onPublishComplete status is " + info.status+"id is "+info.id + "进度"+info.percent);
}
@Override
public void onPublishComplete(UploaderInfo info) {
toastMessage(true, "上传完成,mediaId is"+info.mediaId);
Log.d("uploader", "upload file onPublishComplete status is " + info.status+"id is "+info.id + "进度"+info.percent);
}
@Override
public void onError(UploaderInfo info) {
toastMessage(true, "上传失败,error is"+info.errMsg);
}
});
接口描述-UploaderInfo
参数 | 类型 | 说明 |
---|---|---|
id | string | 文件上传 id |
name | string | 文件名 |
size | string | 文件大小 |
fileType | string | 文件类型 |
suffixName | string | 文件后缀名 |
status | string | 上传状态 |
percent | number | 文件上传进度百分比, 范围:[0, 100] |
errMsg | string | 上传失败的错误信息 |
mediaId | string | 服务端保存的媒资 Id,当上传完成后才有值 |