To upload a short video is to upload a short video file from a client to ApsaraVideo
VOD. The videoId
parameter is returned after the short video is uploaded by using the upload SDK.
If ApsaraVideo Player is integrated, the video ID can be used to configure playback
based on video IDs and playback credentials. This way, you do not need to obtain the
playback URL and credential. This topic describes how to upload a short video by using
the upload SDK for Android.
Background information
VODSVideoUploadClient
is an upload instance that is used to upload short videos from an Android client.
You are allowed to upload a short video and a thumbnail at the same time. You can
use only a Security Token Service (STS) token to upload a short video. If you need
to use upload URLs and credentials, you must separately upload the video and the thumbnail
by using VODUploadClient
. For more information, see Upload a file.
Upload a file
- Obtain an STS token. For more information, see Obtain an STS token.
- Declare a
VODSVideoUploadClient
object. The object cannot be a local variable. // Initialize the VODSVideoUploadClient object.
VODSVideoUploadClient vodsVideoUploadClient = new VODSVideoUploadClientImpl(this.getApplicationContext());
vodsVideoUploadClient.init();
- Construct upload parameters.
// Construct upload parameters.
// Make sure that the parameter settings are valid. Otherwise, an exception is thrown in the SDK.
// Make sure that the path of the file to be uploaded is valid. In addition, you must obtain the read and write permissions on the file because sensitive permissions need to be dynamically requested in Android 6.0 or later.
VodHttpClientConfig vodHttpClientConfig = new VodHttpClientConfig.Builder()
.setMaxRetryCount(2)// Specify the maximum number of retries.
.setConnectionTimeout(15 * 1000)// Specify the connection timeout period.
.setSocketTimeout(15 * 1000)// Specify the timeout period for the socket connection.
.build();
// Construct the information about the short video, including the description, title, and details.
SvideoInfo svideoInfo = new SvideoInfo();
svideoInfo.setTitle(new File(videoPath).getName());// Specify the title.
svideoInfo.setDesc("");// Specify the description.
svideoInfo.setCateId(1);// Specify the category ID.
// Construct parameters for uploading the short video to ApsaraVideo VOD. This step is important.
VodSessionCreateInfo vodSessionCreateInfo =new VodSessionCreateInfo.Builder()
.setImagePath(imagePath)// Specify the path of the thumbnail.
.setVideoPath(videoPath)// Specify the path of the video.
.setAccessKeyId(accessKeyId)// Specify a temporary AccessKey ID.
.setAccessKeySecret(accessKeySecret)// Specify a temporary AccessKey secret.
.setSecurityToken(securityToken)// Specify the STS token.
.setExpriedTime(expriedTime)// Specify the expiration time for the STS token.
.setRequestID(requestID)// Specify the request ID. You can determine whether to obtain the request ID returned by STS.
.setIsTranscode(true)// Specify whether to enable transcoding. If transcoding is enabled, you must register a listener on the AppServer to receive notifications when the transcoding succeeds on the ApsaraVideo VOD server.
.setSvideoInfo(svideoInfo)// Specify the information about the short video.
.setVodHttpClientConfig(vodHttpClientConfig)// Specify network parameters.
.build();
- Set callbacks and start the upload.
You must set the VODSVideoUploadCallback
callback before you start the upload.
vodsVideoUploadClient.uploadWithVideoAndImg(vodSessionCreateInfo, new VODSVideoUploadCallback() {
@Override
public void onUploadSucceed(String videoId, String imageUrl) {
// This callback is fired when the upload succeeds. The video ID and image URL are returned.
Log.d(TAG,"onUploadSucceed"+ "videoId:"+ videoId + "imageUrl" + imageUrl);
}
@Override
public void onUploadFailed(String code, String message) {
// This callback is fired when the upload fails. The error code and error message are returned. You must carefully read the error code and error message.
Log.d(TAG,"onUploadFailed" + "code" + code + "message" + message);
}
@Override
public void onUploadProgress(long uploadedSize, long totalSize) {
// This callback is fired when the default or custom upload progress is reached. The current thread is not a UI thread.
Log.d(TAG,"onUploadProgress" + uploadedSize * 100 / totalSize);
progress = uploadedSize * 100 / totalSize;
handler.sendEmptyMessage(0);
}
@Override
public void onSTSTokenExpried() {
Log.d(TAG,"onSTSTokenExpried");
// This callback is fired when the STS token expires. If the file is being uploaded when the STS token expires, resumable upload is performed after the STS token is updated.
vodsVideoUploadClient.refreshSTSToken(accessKeyId,accessKeySecret,securityToken,expriedTime);
}
@Override
public void onUploadRetry(String code, String message) {
// This callback is fired when the system retries the upload.
Log.d(TAG,"onUploadRetry" + "code" + code + "message" + message);
}
@Override
public void onUploadRetryResume() {
// This callback is fired when the upload retry succeeds and the system resumes the upload. You are notified of the successful retry.
Log.d(TAG,"onUploadRetryResume");
}
});
Control the upload
You can call the following methods to control the upload in the upload instance VODSVideoUploadClient
:
- Pause the upload.
// This method must be set in pairs with the vodsVideoUploadClient.resume() method.
vodsVideoUploadClient.pause();
- Resume the upload.
// This method must be set in pairs with the vodsVideoUploadClient.pause() method.
vodsVideoUploadClient.resume();
- Cancel the upload.
// If you cancel an upload, the upload process ends and you cannot call the vodsVideoUploadClient.resume() method to resume the upload.
vodsVideoUploadClient.cancel();
Handle callbacks
- onUploadProgress
The onUploadProgress
callback is fired each time a part is uploaded. The callback parameters include uploadedSize
and totalSize
. The value of the uploadedSize parameter indicates the size of uploaded parts. The
value of the totalSize parameter indicates the total size of the file that you upload.
- onUploadSucceed
The onUploadSucceed
callback is fired when the upload succeeds. The callback parameters include videoId
and imageUrl
.
Note
- After the video is uploaded, the videoId parameter is returned. The value of the videoId
parameter indicates the video ID. Then, you can integrate ApsaraVideo Player and play
the video based on the video ID and playback credential. For more information, see
Overview.
- After an image is uploaded, the imageUrl parameter is returned. The value of the imageUrl
parameter indicates the image URL. If URL signing is enabled, the image URL expires
after a specific period of time. For more information, see URL authentication.
- onUploadFailed
The onUploadFailed
callback is fired when the upload fails. You can view the cause of the failure based
on the code
and message
callback parameters. In addition, a prompt is displayed on the web page. For more
information about error codes, see Error codes and Handle exceptions.
- onSTSTokenExpired
The
onSTSTokenExpried
callback is fired when the STS token expires. You can send a request to the AppServer
to obtain a new STS token and call the following method to resume the upload:
refreshSTSToken(accessKeyId,accessKeySecret,securityToken,expriedTime);
- uploadRetry
When the upload times out, the uploadRetry
callback is fired and the system automatically retries to upload the file. You can
receive a prompt on the web page or call the cancel
method to cancel the upload. In addition, you can set the maxRetryCount
parameter to specify the maximum number of retries. If the retry result indicates
that the upload can be resumed, the uploadRetryResume
callback is fired and the upload is resumed.