Java实现阿里云视频点播的功能

获取视频的播放地址

1.在阿里云中开通视频点播功能,上传一个测试视频
Java实现阿里云视频点播的功能_第1张图片
视频基本信息:(视频ID)
Java实现阿里云视频点播的功能_第2张图片

2.创建一个maven项目,引入依赖

<dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
        </dependency>

        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
        </dependency>

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-vod</artifactId>
        </dependency>

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

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>
    </dependencies>

3.根据你自己的accessKeyId和accessKeySecret获取client (不知到阿里云accessKeyId和accessKeySecret的可以参照前面的OSS文章提到过怎么获取)

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

4.获取视频的播放地址

public class testVod {
     
    public static void main(String[] args) {
     
        try {
     
            DefaultAcsClient client = InitObject.initVodClient("your accessKeyId","your accessKeySecret");
            //创建获取视频地址的request和response
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            GetPlayInfoResponse response = new GetPlayInfoResponse();

            //向request中传入视频id
            request.setVideoId("d6f12144360449b1935f01bb73e1f3fa");
            //调用初始化对象里方法,传递request,获取数据
            response = client.getAcsResponse(request);

            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();

            for (GetPlayInfoResponse.PlayInfo playInfo:playInfoList
                 ) {
     
                System.out.println("URL= "+ playInfo.getPlayURL());
            }
            System.out.println("VidoBase title = "+response.getVideoBase().getTitle());

        } catch (ClientException e) {
     
            e.printStackTrace();
        }
    }
}

5.结果,根据获取的URL可以直接播放
在这里插入图片描述
(在这里获取的是不加密的视频)

获取视频播放凭证(播放加密转码后的视频)

public static void main(String[] args) {
     
        //根据视频的ID获取视频的播放凭证
        try {
     
            DefaultAcsClient client = InitObject.initVodClient("accessKeyId","accessKeySecret");
            //创建获取视频凭证的request和response
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();

            //向request中传入视频id
            request.setVideoId("c1fc08a30da742a7af6d4a891a63c9a8");
            // //调用初始化对象里方法获取凭证
            response = client.getAcsResponse(request);
            System.out.println("凭证= "+ response.getPlayAuth());

        } catch (ClientException e) {
     
            e.printStackTrace();
        }

    }

在这里插入图片描述

上传视频到阿里云视频点播服务

public static void main(String[] args) {
     
        //本地上传
        UploadVideoRequest request = new UploadVideoRequest("accessKeyId", "accessKeySecret", "测试视频", "C:/Users/jaksiont/Desktop/test.mp4");
        /* 可指定分片上传时每个分片的大小,默认为2M字节 */
        request.setPartSize(2 * 1024 * 1024L);
        /* 可指定分片上传时的并发线程数,默认为1,(注:该配置会占用服务器CPU资源,需根据服务器情况指定)*/
        request.setTaskNum(1);

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

    }

Java实现阿里云视频点播的功能_第3张图片

如果项目中做了Nginx代理一定要注意

问题:我在测试上传视频时,总是出现跨域问题,可是检查了好久,跨域的配置都是正确的。一开始以为是Nginx修改了配置文件没有生效,可是测试了很久没毛病啊,终于在今天早上找到了原因配置nginx上传文件大小,否则上传时会有 413 (Request Entity Too Large) 异常
Java实现阿里云视频点播的功能_第4张图片client_max_body_size 1024m;一定要在配置中加上这句,不加的话由于文件限制,Nginx就不做请求转发了!!!!!!

你可能感兴趣的:(Vod,Java,阿里云)