本文主要介绍了轮播台API的调用场景和代码示例。

场景描述

轮播台API的调用场景:
  • 将三个点播文件A、B、C按照顺序分别添加到节目单中。
  • 添加完成,对单个节目的播放次数和整体节目单的播放次数分别进行配置。
  • 配置完成,查询整个节目单播放列表信息并开启播放。

Java代码示例

package com.alibaba.aliyundebug.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.live20161101.Client;
import com.aliyuncs.AcsResponse;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.live.model.v20161101.*;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.utils.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Random;
import java.util.UUID;

public class Bodan {
    private final static String ACCESS_KEY_ID = "xxxx";
    private final static String ACCESS_KEY_SECRET = "xxxx";
    private final static String testCasterId = "xxxx";


    public static DefaultAcsClient initClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }


    private static CreateCasterResponse createCaster(DefaultAcsClient client) throws ClientException {
        CreateCasterRequest request = new CreateCasterRequest();

        Random random = new Random();
        int i = random.nextInt(100000);
        request.setCasterName("casterName" + i);

        UUID uuid = UUID.randomUUID();
        String token = "thisIsaToken";
        String clientToken = DigestUtils.sha256Hex(token + uuid);
        request.setClientToken(clientToken);

        // 节目单可以只用创建播单型即可。
        request.setNormType(6);
        request.setChargeType("PostPaid");

        CreateCasterResponse response = client.getAcsResponse(request);

        System.out.println("创建导播台成功,返回值: " + JSON.toJSONString(response));
        return response;
    }


    private static void setCasterConfig(DefaultAcsClient client, String casterId) throws ClientException {
        SetCasterConfigRequest request = new SetCasterConfigRequest();
        request.setCasterId(casterId);

        // 配置域名
        request.setDomainName("example.aliyundoc.com");
        JSONObject transcodeConfig = new JSONObject();
        JSONArray liveTemplate = new JSONArray();
        //配置导播台模板
        liveTemplate.add("lld");
        transcodeConfig.put("LiveTemplate", liveTemplate);
        //配置转码模板
        transcodeConfig.put("CasterTemplate", "lp_sd");
        request.setTranscodeConfig(transcodeConfig.toJSONString());

        SetCasterConfigResponse response = client.getAcsResponse(request);
        System.out.println("添加导播台配置成功:requestId" + response.getRequestId() + " response:" + JSON.toJSONString(response));

    }

    private static AcsResponse startCaster(DefaultAcsClient client, String casterId) throws ClientException {
        StartCasterRequest startCasterRequest = new StartCasterRequest();
        startCasterRequest.setCasterId(casterId);
        StartCasterResponse acsResponse = client.getAcsResponse(startCasterRequest);
        System.out.println("开启导播台成功");
        return acsResponse;
    }

    private static AcsResponse stopCaster(DefaultAcsClient client, String casterId) throws ClientException {
        StopCasterRequest stopCasterRequest = new StopCasterRequest();
        stopCasterRequest.setCasterId(casterId);
        StopCasterResponse acsResponse = client.getAcsResponse(stopCasterRequest);
        System.out.println("停止导播台成功");
        return acsResponse;
    }

    private static CommonResponse addShowIntoShowList(DefaultAcsClient client, String showName, String resourceId, String resourceUrl,
                                                      String resourceType, Integer spot, Integer repeatTimes,
                                                      String casterId, Long duration) throws ClientException {
        CommonRequest addShowIntoShowListRequest = new CommonRequest();
        addShowIntoShowListRequest.setSysDomain("live.aliyuncs.com");
        addShowIntoShowListRequest.setSysVersion("2016-11-01");
        addShowIntoShowListRequest.setSysAction("AddShowIntoShowList");


    
        if (casterId == null || resourceType == null) {
            return null;
        }
        if (resourceId == null && resourceUrl == null) {
            return null;
        }
        addShowIntoShowListRequest.putQueryParameter("CasterId", casterId);
        addShowIntoShowListRequest.putQueryParameter("ResourceType", resourceType);
        if (showName != null) {
            addShowIntoShowListRequest.putQueryParameter("ShowName", showName);
        }
        if (resourceUrl != null) {
            addShowIntoShowListRequest.putQueryParameter("ResourceUrl", resourceUrl);
        }
        if (resourceId != null) {
            addShowIntoShowListRequest.putQueryParameter("ResourceId", resourceId);
        }
        if (spot != null) {
            addShowIntoShowListRequest.putQueryParameter("Spot", spot.toString());
        }
        if (repeatTimes != null) {
            addShowIntoShowListRequest.putQueryParameter("RepeatTimes", repeatTimes.toString());
        }
        if (duration != null) {
            addShowIntoShowListRequest.putQueryParameter("Duration", duration.toString());
        }
        CommonResponse addShowIntoShowListResponse = client.getCommonResponse(addShowIntoShowListRequest);
        return addShowIntoShowListResponse;
    }


    private static void removeShowFromShowList(DefaultAcsClient client, String casterId, String showId) throws ClientException {

        CommonRequest removeShowFromShowListFromShowList = new CommonRequest();
        removeShowFromShowListFromShowList.setSysDomain("live.aliyuncs.com");
        removeShowFromShowListFromShowList.setSysVersion("2016-11-01");
        removeShowFromShowListFromShowList.setSysAction("RemoveShowFromShowList");

        removeShowFromShowListFromShowList.putQueryParameter("ShowId", showId);
        removeShowFromShowListFromShowList.putQueryParameter("CasterId", casterId);

        CommonResponse removeShowFromShowListFromShowListResponse = client.getCommonResponse(removeShowFromShowListFromShowList);
        System.out.println("删除导播台节目,removeShowFromShowListFromShowListResponse:" + JSON.toJSONString(removeShowFromShowListFromShowListResponse));
    }

    private static CommonResponse modfiyShowList(DefaultAcsClient client, String casterId, String showId, Integer repeatTimes, Integer spot,
                                                 String highPriorityShowId, String highPriorityShowStartTime) throws ClientException {

        CommonRequest modfiyShowListRequest = new CommonRequest();
        modfiyShowListRequest.setSysDomain("live.aliyuncs.com");
        modfiyShowListRequest.setSysVersion("2016-11-01");
        modfiyShowListRequest.setSysAction("ModifyShowList");
        if (casterId != null) {
            modfiyShowListRequest.putQueryParameter("CasterId", casterId);
        }
        if (showId != null) {
            modfiyShowListRequest.putQueryParameter("ShowId", showId);
        }
        if (repeatTimes != null) {
            modfiyShowListRequest.putQueryParameter("RepeatTimes", repeatTimes.toString());
        }
        if (spot != null) {
            modfiyShowListRequest.putQueryParameter("Spot", spot.toString());
        }
        if (highPriorityShowId != null) {
            modfiyShowListRequest.putQueryParameter("HighPriorityShowId", highPriorityShowId.toString());
        }
        if (highPriorityShowStartTime != null) {
            modfiyShowListRequest.putQueryParameter("HighPriorityShowStartTime", highPriorityShowStartTime.toString());
        }
        CommonResponse modfiyShowListResponse = client.getCommonResponse(modfiyShowListRequest);
        System.out.println("修改showList成功,返回值:" + JSON.toJSONString(modfiyShowListResponse));
        return modfiyShowListResponse;

    }

    private static CommonResponse describeShowList(DefaultAcsClient client, String casterId) throws ClientException {

        CommonRequest describeShowListRequest = new CommonRequest();
        describeShowListRequest.setSysDomain("live.aliyuncs.com");
        describeShowListRequest.setSysVersion("2016-11-01");
        describeShowListRequest.setSysAction("DescribeShowList");
        if (casterId != null) {
            describeShowListRequest.putQueryParameter("CasterId", casterId);
        }
        CommonResponse describeShowListRequestResponse = client.getCommonResponse(describeShowListRequest);
        System.out.println("查询showList成功,返回值:" + JSON.toJSONString(describeShowListRequestResponse));
        return describeShowListRequestResponse;

    }

    private static void playChoosenShow(DefaultAcsClient client, String casterId, String showId) throws ClientException {
        CommonRequest playChoosenShow = new CommonRequest();
        playChoosenShow.setSysDomain("live.aliyuncs.com");
        playChoosenShow.setSysVersion("2016-11-01");
        playChoosenShow.setSysAction("playChoosenShow");
        playChoosenShow.putQueryParameter("CasterId", casterId);
        playChoosenShow.putQueryParameter("ShowId", showId);
        CommonResponse playChoosenShowResponse = client.getCommonResponse(playChoosenShow);
        System.out.println("手动切换showId: "+showId+" 成功");
    }


    private static List<DescribeCasterStreamUrlResponse.CasterStream> describeCasterStreamUrl(DefaultAcsClient client, String casterId) throws ClientException {
        DescribeCasterStreamUrlRequest describeCasterStreamUrlRequest = new DescribeCasterStreamUrlRequest();
        describeCasterStreamUrlRequest.setCasterId(casterId);
        DescribeCasterStreamUrlResponse acsResponse = client.getAcsResponse(describeCasterStreamUrlRequest);
        List<DescribeCasterStreamUrlResponse.CasterStream> casterStreams = acsResponse.getCasterStreams();
        return casterStreams;
    }

    public static void main(String[] args) throws ClientException {
        DefaultAcsClient client = null;
        try {
            client = initClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);
            //创建导播台
            CreateCasterResponse caster = createCaster(client);
            String casterId = caster.getCasterId();
            //设置导播台
            setCasterConfig(client, casterId);

            //添加三个点播文件,让第一个文件播一遍,第二个文件播两遍,第三个文件播三遍
            String[] resourceIds = new String[]{"698d2b23581f476ea71107703e64****", "9c97e83e211a435b9f797e4e20ee****", "76c6addaa41c438985666a8a964f****"};
            for (int i = 0; i < resourceIds.length; i++) {
                String showName = "ShowName#" + i;
                Integer repeatTimes = i;
                addShowIntoShowList(client, showName, resourceIds[i], null, "vod", null, repeatTimes, casterId, null);
            }
            modfiyShowList(client, casterId, null, 10, null, null, null);
            CommonResponse describeShowList = describeShowList(client, casterId);
            String data = describeShowList.getData();
            JSONObject jsonObject = JSON.parseObject(data);
            JSONObject showList = jsonObject.getJSONObject("ShowList");
            JSONArray shows = showList.getJSONArray("Shows");
            //打印播放节目单
            for (int i = 0; i < shows.size(); i++) {
                JSONObject show = (JSONObject) shows.get(i);
                String showId = show.getString("ShowId");
                String resourceType = show.getString("ResourceType");
                String resourceInfo = show.getString("ResourceInfo");
                Integer repeatTimes = show.getInteger("RepeatTimes");
                Long duration = show.getLong("Duration");
                String showInfo = String.format("show%d: showId: %s \n resourceType: %s  \n  resourceInfo: %s \n RepeatTimes: %d \n Duration: %d",
                        i + 1, showId, resourceType, resourceInfo, repeatTimes, duration);
                System.out.println(showInfo);
            }

            startCaster(client, casterId);

            Thread.sleep(500000);

            stopCaster(client, casterId);


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