本文以Java语言示例,为您介绍了调用StartMPUTask实现开始任务功能。

前提条件

在进行开始任务之前,请确保您已经完成以下操作:

接口说明

调用StartMPUTask实现开始任务,具体接口信息请参见StartMPUTask

方法示例

您需要在pom.xml文件中添加依赖。
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-rtc</artifactId>
    <version>1.0.4</version>
</dependency>
说明 不同语言的版本号,请参见阿里云SDK中心
import java.util.List;
import java.util.ArrayList;
import com.aliyuncs.*;
import com.aliyuncs.profile.*;
import com.aliyuncs.http.*;
import com.aliyuncs.rtc.model.v20180111.*;
import com.aliyuncs.exceptions.*;
public class StartTask {
private String regionID = "yourRegionId"; //例如:cn-hangzhou
private String endpoint = "rtc.aliyuncs.com";
private String accessKeyID = "yourAccessId";
private String accessKeySecret = "yourAccessSecret";
private String startTask(String appID, String channelId) throws ClientException {
            try {
            DefaultProfile profile = DefaultProfile.getProfile(regionID, accessKeyID, accessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            StartMPUTaskRequest request = new StartMPUTaskRequest();
            request.setAppId(appID);
            DefaultProfile.addEndpoint(regionID, regionID, request.getProduct(), endpoint);
            request.setProtocol(ProtocolType.HTTP);
            request.setChannelId(channelId);
            request.setTaskId("unique task ID");
            //混流的编码参数 0:audio base; 1:360P,500kbps, 15fps; 10: 540P,700kbps, 24fps; 20:720P,1Mbps, 25fps; 30:1080P,2Mbps,30fps
            request.setMediaEncode(2);
            // 背景色使用RGB颜色,0为黑色
            request.setBackgroundColor(0);
            // 需要开通阿里云直播服务,并根据签名规则生成的推流的URL
            request.setStreamURL("rtmp://url/app/stream");
            //布局的说明
            List<Long> layoutIds = new ArrayList<>();
            layoutIds.add(2);
            layoutIds.add(3);
            request.setLayoutIdss(layoutIds);
            StartMPUTaskResponse response = client.getAcsResponse(request);
            return response.getRequestId();
            } catch (ClientException ex) {
            ex.printStackTrace();
            return "";
        }
    }
}