阿里云官网开通视频点播(推荐按照流量计费,不用不花钱)。

image-20220121212920492

阿里云视频点播服务提供API与SDK。API就是提供一些视频操作(上传、下载...)的固定url,我们通过提供的url拼接参数进行操作。SDK对API方式进行了封装,我们直接调用SDK中的类与方法来实现功能,使用更加方便,官方推荐使用。

在阿里云官网有具体的入门教程https://help.aliyun.com/document_detail/57756.htm。下面写一些简单demo入门。

在demo前,先提示一个细节:因为视频有加密的方式,加密视频不可以通过地址直接访问,因此我们在数据库中不存储视频的地址,而存储视频的Id。通过Id可以拿到视频的播放地址、凭证等信息。

(1)引入依赖

service下新建Maven子模块service_vod。在其中pom.xml中引入依赖。

 <!--   视频点播对maven仓库的配置和依赖     -->
<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>    

 <!--   jar包依赖     -->
<dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-vod</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
</dependencies>

(2)初始化操作

由于我们现在是demo,在test目录下新建com.wangzhou.vodtest包。InitObject类中创建DefaultAcsClient对象

package com.wangzhou.vodtest;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;

//初始化类
public class InitObject {

    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;
    }

}

测试类。

public class TestVod {

    public static void main(String[] args) throws ClientException {

        // 1.创建客户端
        DefaultAcsClient client = InitObject.initVodClient("xxxx", "xxxx");
        // 2.创建request、response
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        GetPlayInfoResponse response = new GetPlayInfoResponse();

        // 3.设置视频Id值
        request.setVideoId("xxxx");

        // 4.获取response
        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");//VideoBase.Title = 6 - What If I Want to Move Faster.mp4
    }

}