编程语言
首页 > 编程语言> > java 对接阿里视频点播服务工具类

java 对接阿里视频点播服务工具类

作者:互联网

package com.kindess.util;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.*;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;

import java.io.InputStream;
import java.util.List;

/**
 * @author kindess
 * @date 2019-07-29
 * @return VodUtil
 */
public class VodUtil {

    /**
     * AccessKeyId
     */
    private static final String ACCESSKEYID = "你的ACCESSKEYID ";

    /**
     * AccessKeySecret
     */
    private static final String ACCESSKEYSERTET = "你的ACCESSKEYSERTET ";

    /**
     * 初始化点播服务
     *
     * @return
     * @throws ClientException
     */
    public static DefaultAcsClient initVodClient() throws ClientException {
        // 点播服务接入区域
        String regionId = "cn-shanghai";
        DefaultProfile profile = DefaultProfile.getProfile(regionId, ACCESSKEYID, ACCESSKEYSERTET);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * 获取播放地址函数
     *
     * @param videoId
     * @throws Exception
     */
    public static GetPlayInfoResponse getPlayInfo(String videoId) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        return VodUtil.initVodClient().getAcsResponse(request);
    }

    /**
     * 播放方法
     *
     * @param videoId
     * @return
     */
    public static String getPlay(String videoId) {
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        try {
            response = VodUtil.getPlayInfo(videoId);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                return playInfo.getPlayURL();
            }
        } catch (Exception e) {
            e.getLocalizedMessage();
        }
        return null;
    }

    /**
     * 流式上传接口
     *
     * @param title
     * @param fileName
     * @param inputStream
     */
    public static String uploadStream(String title, String fileName, InputStream inputStream) {
        UploadStreamRequest request = new UploadStreamRequest(ACCESSKEYID, ACCESSKEYSERTET, title, fileName, inputStream);
        //视频标签
        request.setTags("课程视频");
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        //请求视频点播服务的请求ID
        if (response.isSuccess()) {
            return response.getVideoId();
        } else {
            return null;
        }
    }

    /**
     * 删除视频
     *
     * @param client 发送请求客户端
     * @return DeleteVideoResponse 删除视频响应数据
     * @throws Exception
     */
    public static DeleteVideoResponse deleteVideo(DefaultAcsClient client, String videoId) throws Exception {
        DeleteVideoRequest request = new DeleteVideoRequest();
        //支持传入多个视频ID,多个用逗号分隔
        request.setVideoIds(videoId);
        return client.getAcsResponse(request);
    }

    /**
     * 删除方法
     *
     * @param videoId
     */
    public static void delVideo(String videoId) {
        DefaultAcsClient client = null;
        DeleteVideoResponse response = new DeleteVideoResponse();
        try {
            client = initVodClient();
            response = deleteVideo(client, videoId);
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

标签:return,String,java,request,对接,videoId,import,视频点播,com
来源: https://blog.csdn.net/weixin_41628125/article/details/100020614