本示例介绍使用Alibaba Cloud SDK for Java调用AddCasterComponent接口添加组件。

前提条件

请在pom.xml文件中增加以下依赖,准确的SDK版本号,参见阿里云开发工具包(SDK)

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>java.demo</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-live -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-live</artifactId>
            <version>3.7.5</version>
        </dependency>
    </dependencies>
</project>

示例代码

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.live.model.v20161101.AddCasterComponentRequest;
import com.aliyuncs.live.model.v20161101.AddCasterComponentResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;


/**
 * 添加组件
 */
public class TestAddCasterComponent {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>", // 地域ID,可以通过接口 DescribeRegions 查看可用的地域ID。
                "<your-access-key-id>", // 您的AccessKey ID
                "<your-access-key-secret>"); // 您的AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);

        AddCasterComponentRequest request = new AddCasterComponentRequest();
        // 导播间Id。
        request.setCasterId("yourCasterId");
        // 组件类型,只能是预设值的一种: “text”/“image”等。
        request.setComponentType("image");
        // 如果是image,设置该Layer元素属性,JSON格式字符串,参数名采用首字母大写,驼峰格式
        request.setImageLayerContent("yourImageLayerContent");
        // 如果是text,设置该Layer元素属性,JSON格式字符串,参数名采用首字母大写,驼峰格式
        // request.setTextLayerContent("yourImageLayerContent");
        // 用于指定组件位置,每个位置至多设置一个组件,格式需符合“RC01…RC12”
        request.setLocationId("RC01");
        // 设置该组件Layer的尺寸,布局等信息,JSON格式字符串,参数名采用首字母大写、驼峰格式
        /**
         * ComponentLayer元素
         *         HeightNormalized:设置该layer元素的高度归一化比例值,其中元素的宽度会按照该高度进行等比缩放。
         * WidthNormalized:设置该layer元素的宽度归一化比例值,其中元素的高度会按照该宽度进行等比缩放。
         *         WidthNormalized和heightNormalized有冲突,一旦同时设置了heightNormalized和widthNormalized,只有heightNormalized生效。
         *         如果只能设置heightNormalized和widthNormalized中的一个,后设置的值会影响前面的设置。
         * PositionRefer元素
         *         设置元素的position 参考坐标值
         *         取值范围:”topLeft”、”topRight”、”bottomLeft”、”bottomRight”
         */
        request.setComponentLayer(
            "{ HeightNormalized:0, PositionRefer:\"topLeft\" }");

        try {
            // 发起请求并获取返回结果
            AddCasterComponentResponse response = client.getAcsResponse(request);
            // 处理业务逻辑
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}