JAVA之阿里云视频点播篇

一、依赖引入

        <!--阿里云oss存储-->
        <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>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-vod</artifactId>
            <version>2.15.2</version>
        </dependency>

二、代码区

    private static String REGION_ID = "上传点播的地域";
    /** 阿里云API的密钥Access Key ID */
    private static String accessKeyId ="";
    /** 阿里云API的密钥Access Key Secret */
    private static String accessKeySecret = "";
    //初始化
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) {
        //点播服务所在的Region,国内请填cn-shanghai,不要填写别的区域
        DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * 刷新视频上传凭证
     * @param client 发送请求客户端
     * @return RefreshUploadVideoResponse 刷新视频上传凭证响应数据
     * @throws Exception
     */
    public static RefreshUploadVideoResponse refreshUploadVideo(DefaultAcsClient client, String videoId) throws Exception {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        request.setVideoId("VideoId");
        return client.getAcsResponse(request);
    }

    /**
     * 获取视频上传地址和凭证
     * @param client 发送请求客户端
     * @return CreateUploadVideoResponse 获取视频上传地址和凭证响应数据
     * @throws Exception
     */
    public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient client) throws Exception {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle("this is a sample");
        request.setFileName("filename.mp4");
        JSONObject userData = new JSONObject();
        JSONObject messageCallback = new JSONObject();
        messageCallback.put("CallbackURL", "http://xxxxx");
        messageCallback.put("CallbackType", "http");
        userData.put("MessageCallback", messageCallback.toJSONString());
        JSONObject extend = new JSONObject();
        extend.put("MyId", "user-defined-id");
        userData.put("Extend", extend.toJSONString());
        request.setUserData(userData.toJSONString());
        return client.getAcsResponse(request);
    }



    public static Map getAuth(String title) throws Exception{
        Map map = new HashMap();
        DefaultAcsClient client = AliyunuploadClientUtils.initVodClient(accessKeyId, accessKeySecret);
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        //将以下信息替换成前端动态传入的文件信息即可
        //上传的文件标题
        request.setTitle(title);
        //上传文件的相关藐视
        //request.setDescription("linux");
        //上传文件名
        request.setFileName(title+".mp4");
        CreateUploadVideoResponse response = new CreateUploadVideoResponse();
        try {
            response = client.getAcsResponse(request);
            //将下面是三个参数封装成json串返回给前端,这个json传就相当于一个凭证,该凭证默认的生命周期是3600秒
            //VideoId : + response.getVideoId()
            //UploadAddress: response.getUploadAddress()
            //UploadAuth : + response.getUploadAuth()
            String url = getUrl(response.getVideoId());
            System.out.print("VideoId = " + response.getVideoId() + "\n");
            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
            System.out.print("videoUrl = " + url + "\n");
            map.put("code","0");
            map.put("VideoId",response.getVideoId());
            map.put("UploadAddress",response.getUploadAddress());
            map.put("UploadAuth",response.getUploadAuth());
            map.put("videoUrl",url);
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
            map.put("code","-1");
        }
        return map;
    }

    public static Map getRefreshAuth(String videoId) {
        Map map = new HashMap();
        DefaultAcsClient client = AliyunuploadClientUtils.initVodClient(accessKeyId, accessKeySecret);
        RefreshUploadVideoRequest request=new RefreshUploadVideoRequest();
        request.setVideoId(videoId);

        RefreshUploadVideoResponse response=new RefreshUploadVideoResponse();
        try {
            response=client.getAcsResponse(request);
            String url = getUrl(response.getVideoId());
            System.out.print("VideoId = " + response.getVideoId() + "\n");
            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
            System.out.print("videoUrl = " + url + "\n");
            map.put("code","0");
            map.put("VideoId",response.getVideoId());
            map.put("UploadAddress",response.getUploadAddress());
            map.put("UploadAuth",response.getUploadAuth());
            map.put("videoUrl",url);
        } catch (ClientException | com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
            map.put("code","-1");
        }
        return map;
    }


    // 根据 videoId 获取视频播放地址
    public static String getUrl (String videoId){
        // 创建SubmitMediaInfoJob实例并初始化
        DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, accessKeyId,accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        // 视频ID。
        request.setVideoId(videoId);
        try {
            GetPlayInfoResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            for (GetPlayInfoResponse.PlayInfo playInfo : response.getPlayInfoList()) {
                // 播放地址
                String playURL = playInfo.getPlayURL();
                System.out.println("PlayInfo.PlayURL = " + playURL);
                return playURL;
            }
        } catch (ClientException e) {
            e.getErrorCode();
            e.getErrorMessage();
            e.getRequestId();
        } catch (com.aliyuncs.exceptions.ServerException e) {
            e.printStackTrace();
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
        }
        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);
    }
    /*删除视频详情*/
    public static void deleteCourseDetail(String videoId) {
        DefaultAcsClient client = initVodClient(accessKeyId, accessKeySecret);
        DeleteVideoResponse response = new DeleteVideoResponse();
        try {
            response = deleteVideo(client,videoId);
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容