1. 程式人生 > 實用技巧 >前沿團隊前端程式碼規範

前沿團隊前端程式碼規範

技術標籤:阿里雲視訊點播java

阿里雲視訊點播(測試)

文章目錄

服務端SDK

1. 簡介:

sdk的方式將api進行了進一步的封裝,不用自己建立工具類。我們可以基於服務端SDK編寫程式碼來呼叫點播API,實現對點播產品和服務的快速操作。

2.功能介紹

  • SDK封裝了對API的呼叫請求和響應,避免自行計算較為繁瑣的APIapi簽名。
  • 支援所有點播服務的api,並提供了相應的例項程式碼。
  • 支援7種開發語言,包括:Java,Python,PHP,.NET,Node.js,Go,C/C++.
  • 通常在釋出新的API後,我們會及時同步更新SDK,所以即便您沒有找到對應API的示例程式碼,也可以參考舊的例項自行實現呼叫。

提示:這裡可以新增本文要記錄的大概內容:
例如:隨著人工智慧的不斷髮展,機器學習這門技術也越來越重要,很多人都開啟了學習機器學習,本文就介紹了機器學習的基礎內容。


提示:以下是本篇文章正文內容,下面案例可供參考

一、使用SDK

  1. 安裝:
    參考文件
    新增maven倉庫的配置和依賴到pom
<repositories>
	<repository>
	<id>sonatype-nexus-staging</id>
	<name>
Sonatype Nexus Staging</name> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <
dependency>
<groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.3.3</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-vod</artifactId> <version>2.15.5</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency>
  1. 初始化
    參考文件
    根據文件例項建立AliyunVODSDKUtils.java

public class AliyunVodSDKUtils {
    public static DefaultAcsClient initVodClient(String accessKeyId, String
            accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai"; // 點播服務接入區域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId,
                accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }
}

注意:regionId 是你選擇得到伺服器所在地址,一般選擇距離自己最近的那個。
3. 建立測試類
建立VodSdkTest.java

public class VodSdkTest {
    String accessKeyId = "你的accessKeyId";
    String accessKeySecret = "你的accessKeySecret";
}

二、建立測試用例

參考文件

1.獲取視訊播放憑證

根據文件中的程式碼,修改如下

 /**
     * 獲取視訊播放憑證
     * @throws ClientException
     */
    @Test
    public void testGetVideoPlayAuth() throws ClientException {
//初始化客戶端、請求物件和相應物件
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId,
                accessKeySecret);
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        try {
//設定請求引數
            request.setVideoId("視訊ID");
            //獲取請求響應
            response = client.getAcsResponse(request);
//輸出請求結果
//播放憑證
            System.out.print("PlayAuth = " + response.getPlayAuth() + "\n");
//VideoMeta資訊
            System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle()
                    + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }

2.獲取視訊播放地址

 /**
     * 獲取視訊播放地址
     * @throws ClientException
     */
    @Test
    public void testGetPlayInfo() throws ClientException {
        //初始化客戶端、請求物件和相應物件
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId,
                accessKeySecret);
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        try {
            //設定請求引數
            //注意:這裡只能獲取非加密視訊的播放地址
            request.setVideoId("視訊ID");
            //獲取請求響應
            response = client.getAcsResponse(request);
            //輸出請求結果
            List<GetPlayInfoResponse.PlayInfo> playInfoList =
                    response.getPlayInfoList();
            //播放地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() +
                        "\n");
            }
            //Base資訊
            System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle()
                    + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }

參考文件

三.安裝SDK

  1. 配置pom
   <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.3.3</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun.oss</groupId>
        <artifactId>aliyun-sdk-oss</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-vod</artifactId>
        <version>2.15.2</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.28</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20170516</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.2</version>
    </dependency>
  1. 安裝非開源jar包
    在這裡插入圖片描述
    在本地Maven倉庫中安裝jar包:
    下載視訊上傳SDK,解壓,命令列進入lib目錄,執行以下程式碼
mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-sdk-vod-upload -
Dversion=1.4.11 -Dpackaging=jar -Dfile=aliyun-java-vod-upload-1.4.11.jar

然後在pom中引入jar包

<dependency>
	<groupId>com.aliyun</groupId>
	<artifactId>aliyun-sdk-vod-upload</artifactId>
	<version>1.4.11</version>
</dependency>

三.測試

  1. 建立測試檔案
public class UploadTest {
    //賬號AK資訊請填寫(必選)
    private static final String accessKeyId = "你的accessKeyId";
    //賬號AK資訊請填寫(必選)
    private static final String accessKeySecret = "你的accessKeySecret";
}
  1. 測試本地檔案上傳
 /**
     * 視訊上傳
     */
    @Test
    public void testUploadVideo(){
//1.音視訊上傳-本地檔案上傳
//視訊標題(必選)
        String title = "3 - How Does Project Submission Work - upload by sdk";
//本地檔案上傳和檔案流上傳時,檔名稱為上傳檔案絕對路徑,如:/User/sample/檔名.mp4 (必選)
//檔名必須包含副檔名
        String fileName = "E:/共享/資源/課程視訊/3 - How Does Project Submission
        Work.mp4";
//本地檔案上傳
        UploadVideoRequest request = new UploadVideoRequest(accessKeyId,
                accessKeySecret, title, fileName);
        /* 可指定分片上傳時每個分片的大小,預設為1M位元組 */
        request.setPartSize(1 * 1024 * 1024L);
/* 可指定分片上傳時的併發執行緒數,預設為1,(注:該配置會佔用伺服器CPU資源,需根據服務
器情況指定)*/
        request.setTaskNum(1);
/* 是否開啟斷點續傳, 預設斷點續傳功能關閉。當網路不穩定或者程式崩潰時,再次發起相同上
傳請求,可以繼續未完成的上傳任務,適用於超時3000秒仍不能上傳完成的大檔案。
注意: 斷點續傳開啟後,會在上傳過程中將上傳位置寫入本地磁碟檔案,影響檔案上傳速
度,請您根據實際情況選擇是否開啟*/
        request.setEnableCheckpoint(false);
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadVideoResponse response = uploader.uploadVideo(request);
        System.out.print("RequestId=" + response.getRequestId() + "\n"); //請求視訊點
        播服務的請求ID
        if (response.isSuccess()) {
            System.out.print("VideoId=" + response.getVideoId() + "\n");
        } else {
/* 如果設定回撥URL無效,不影響視訊上傳,可以返回VideoId同時會返回錯誤碼。其他情
況上傳失敗時,VideoId為空,此時需要根據返回錯誤碼分析具體錯誤原因 */
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }
    }

總結

不出意外的話,就成功啦,要是有問題歡迎在下方留言。