合流代碼示例
更新時間:
通過閱讀本文,您可以查看合流代碼的示例。
示例說明
本文以Java語言為例進行介紹,獲取其他語言SDK,請參見視頻直播SDK。
說明
我們認為您在閱讀本文時已了解合流輸出,布局等基本概念。如您暫不清楚相關信息,可參見云端合流概述。
SDK引入
使用Java SDK,詳情請參見Java SDK使用說明。
創(chuàng)建合流任務(使用預設布局)
public void createDefault() throws ClientException {
CreateMixStreamRequest createMixStreamRequest = new CreateMixStreamRequest();
// 設置拉流域名(直播播流域名,即用該域名觀看合流后的內(nèi)容)
createMixStreamRequest.setDomainName("your domain name");
// 設置預設的layoutId,以"MixStreamLayout-2-1"為例
createMixStreamRequest.setLayoutId("MixStreamLayout-2-1");
// 設置輸出配置
JSONObject outputConfig = new JSONObject();
outputConfig.put("AppName", "your app name"); // AppName
outputConfig.put("StreamName", "your stream name"); // StreamName
outputConfig.put("MixStreamTemplate", "lp_ld"); // 合流輸出規(guī)格模板
String outputConfigStr = outputConfig.toJSONString();
createMixStreamRequest.setOutputConfig(outputConfigStr);
// 初始化輸入流
JSONArray inputStreamList = new JSONArray();
// 構建輸入流A
JSONObject inputStreamA = new JSONObject();
inputStreamA.put("LayoutChildId", 1); // 布局id,注意:一定要從1開始,并且所有id要連續(xù)
inputStreamA.put("ResourceType", "url"); // 資源類型: url 或 live
inputStreamA.put("ResourceValue", "your resource value"); // 資源地址
inputStreamList.add(inputStreamA);
// 構建輸入流B(方法同A)
JSONObject inputStreamB = new JSONObject();
inputStreamB.put("LayoutChildId", 2);
inputStreamB.put("ResourceType", "url");
inputStreamB.put("ResourceValue", "your resource value");
inputStreamList.add(inputStreamB);
// 設置輸入流
String inputStreamListStr = inputStreamList.toJSONString();
createMixStreamRequest.setInputStreamList(inputStreamListStr);
// 發(fā)起openapi請求
CreateMixStreamResponse createMixStreamResponse = client.getAcsResponse(createMixStreamRequest);
// 打印合流id
String mixStreamId = createMixStreamResponse.getMixStreamId();
System.out.println(mixStreamId);
}
創(chuàng)建合流任務(自定義布局)
public void createMixStream() throws ClientException {
CreateMixStreamRequest createMixStreamRequest = new CreateMixStreamRequest();
// 設置拉流域名(直播播流域名,即用該域名觀看合流后的內(nèi)容)
createMixStreamRequest.setDomainName("your domain name");
// 設置布局id,自定義布局填寫"USERDEFINED"
createMixStreamRequest.setLayoutId("USERDEFINED");
// 設置輸出配置
JSONObject outputConfig = new JSONObject();
outputConfig.put("AppName", "your app name"); // AppName
outputConfig.put("StreamName", "your stream name"); // StreamName
outputConfig.put("MixStreamTemplate", "lp_ld"); // 合流輸出規(guī)格模板
String outputConfigStr = outputConfig.toJSONString();
createMixStreamRequest.setOutputConfig(outputConfigStr);
// 初始化輸入流
JSONArray inputStreamList = new JSONArray();
// 構建輸入流A
JSONObject inputStreamA = new JSONObject();
JSONObject layoutConfigA = new JSONObject(); // 自定義布局配置設置
Float[] fillPositionNormalizedA = new Float[]{0f, 0f}; // 位置坐標
Float[] fillSizeNormalizedA = new Float[]{1f, 1f}; // 大小歸一化值
layoutConfigA.put("FillMode", "fit");
layoutConfigA.put("PositionRefer", "topLeft");
layoutConfigA.put("FillPositionNormalized", fillPositionNormalizedA);
layoutConfigA.put("FillSizeNormalized", fillSizeNormalizedA);
inputStreamA.put("LayoutChildId", 1); // 布局id,注意:一定要從1開始,并且所有id要連續(xù)
inputStreamA.put("ResourceType", "url"); // 資源類型: url 或 live
inputStreamA.put("ResourceValue", "your resource value"); // 資源地址
inputStreamA.put("LayoutConfig", layoutConfigA); // 設置自定義的布局配置
inputStreamList.add(inputStreamA);
// 構建輸入流B(方法同A)
JSONObject inputStreamB = new JSONObject();
JSONObject layoutConfigB = new JSONObject();
Float[] fillPositionNormalizedB = new Float[]{0.7f, 0.03f};
Float[] fillSizeNormalizedB = new Float[]{0.25f, 0.25f};
layoutConfigB.put("FillMode", "fit");
layoutConfigB.put("PositionRefer", "topLeft");
layoutConfigB.put("FillPositionNormalized", fillPositionNormalizedB);
layoutConfigB.put("FillSizeNormalized", fillSizeNormalizedB);
inputStreamB.put("LayoutChildId", 2);
inputStreamB.put("ResourceType", "url");
inputStreamB.put("ResourceValue", "your resource value");
inputStreamB.put("LayoutConfig", layoutConfigB);
inputStreamList.add(inputStreamB);
// 設置輸入流
String inputStreamListStr = inputStreamList.toJSONString();
createMixStreamRequest.setInputStreamList(inputStreamListStr);
// 發(fā)起openapi請求
CreateMixStreamResponse createMixStreamResponse = client.getAcsResponse(createMixStreamRequest);
// 打印合流id
String mixStreamId = createMixStreamResponse.getMixStreamId();
System.out.println(mixStreamId);
}
更新合流任務
public void updateMixStream() throws ClientException {
UpdateMixStreamRequest updateMixStreamRequest = new UpdateMixStreamRequest();
// 設置拉流域名(直播播流域名,即用該域名觀看合流后的內(nèi)容)
updateMixStreamRequest.setDomainName("your domain name");
// 設置布局id,自定義布局填寫"USERDEFINED"
updateMixStreamRequest.setLayoutId("USERDEFINED");
// 設置要更新的合流id,即之前創(chuàng)建過的合流任務
updateMixStreamRequest.setMixStreamId("your mixstreamId");
// 初始化輸入流
JSONArray inputStreamList = new JSONArray();
// 構建輸入流A
JSONObject inputStreamA = new JSONObject();
JSONObject layoutConfigA = new JSONObject(); // 自定義布局配置設置
Float[] fillPositionNormalizedA = new Float[]{0f, 0f}; // 位置坐標
Float[] fillSizeNormalizedA = new Float[]{1f, 1f}; // 大小歸一化值
layoutConfigA.put("FillMode", "fit");
layoutConfigA.put("PositionRefer", "topLeft");
layoutConfigA.put("FillPositionNormalized", fillPositionNormalizedA);
layoutConfigA.put("FillSizeNormalized", fillSizeNormalizedA);
inputStreamA.put("LayoutChildId", 1); // 布局id,注意:一定要從1開始,并且所有id要連續(xù)
inputStreamA.put("ResourceType", "url"); // 資源類型: url 或 live
inputStreamA.put("ResourceValue", "your resource value"); // 資源地址
inputStreamA.put("LayoutConfig", layoutConfigA); // 設置自定義的布局配置
inputStreamList.add(inputStreamA);
// 構建輸入流B(方法同A)
JSONObject inputStreamB = new JSONObject();
JSONObject layoutConfigB = new JSONObject();
Float[] fillPositionNormalizedB = new Float[]{0.5f, 0.5f};
Float[] fillSizeNormalizedB = new Float[]{0.3f, 0.3f};
layoutConfigB.put("FillMode", "fit");
layoutConfigB.put("PositionRefer", "topLeft");
layoutConfigB.put("FillPositionNormalized", fillPositionNormalizedB);
layoutConfigB.put("FillSizeNormalized", fillSizeNormalizedB);
inputStreamB.put("LayoutChildId", 2);
inputStreamB.put("ResourceType", "url");
inputStreamB.put("ResourceValue", "your resource value");
inputStreamB.put("LayoutConfig", layoutConfigB);
inputStreamList.add(inputStreamB);
// 設置輸入流
String inputStreamListStr = inputStreamList.toJSONString();
updateMixStreamRequest.setInputStreamList(inputStreamListStr);
// 發(fā)起openapi請求
UpdateMixStreamResponse updateMixStreamResponse = client.getAcsResponse(updateMixStreamRequest);
String mixStreamId = updateMixStreamResponse.getMixStreamId();
// 打印合流id
System.out.println(mixStreamId);
}
查詢合流任務
public void describeMixStreamList() throws ClientException {
DescribeMixStreamListRequest describeMixStreamListRequest = new DescribeMixStreamListRequest();
// 設置拉流域名(直播播流域名,即用該域名觀看合流后的內(nèi)容)
describeMixStreamListRequest.setDomainName("your domain name");
// 設置appName
describeMixStreamListRequest.setAppName("your app name");
// 設置查詢開始時間
describeMixStreamListRequest.setStartTime("2020-12-03T14:00:00Z");
// 設置查詢結束時間
describeMixStreamListRequest.setEndTime("2020-12-05T14:00:00Z");
// 發(fā)起openapi請求
DescribeMixStreamListResponse describeMixStreamListResponse = client.getAcsResponse(describeMixStreamListRequest);
// 獲取合流列表
List<MixStreamInfo> result = describeMixStreamListResponse.getMixStreamList();
// 打印合流id
if (result != null && !result.isEmpty()) {
for (MixStreamInfo mixStreamInfo : result) {
String mixStreamId = mixStreamInfo.getMixstreamId();
System.out.println(mixStreamId);
}
}
}
刪除合流任務
public void deleteMixStream() throws ClientException {
DeleteMixStreamRequest deleteMixStreamRequest = new DeleteMixStreamRequest();
// 設置拉流域名(直播播流域名,此前用該域名觀看合流后的內(nèi)容)
deleteMixStreamRequest.setDomainName("your domain name");
// 設置appName
deleteMixStreamRequest.setAppName("your app name");
// 設置流名
deleteMixStreamRequest.setStreamName("your stream name");
// 設置要刪除的合流id
deleteMixStreamRequest.setMixStreamId("your mixstreamId");
// 發(fā)起openapi請求
DeleteMixStreamResponse deleteMixStreamResponse = client.getAcsResponse(deleteMixStreamRequest);
String mixStreamId = deleteMixStreamResponse.getMixStreamId();
// 打印合流id
System.out.println(mixStreamId);
}
相關API
CreateMixStream(創(chuàng)建合流任務)
UpdateMixStream(更新合流任務)
DescribeMixStreamList(查詢合流任務列表)
DeleteMixStream(刪除合流任務)
文檔內(nèi)容是否對您有幫助?