本文介紹通過調用播單型導播臺API實現直播場景。超快捷聚合多個點播視頻,創建播單型導播臺類型直播間,幫助各合作伙伴豐富其視頻場景和內容形態,解決直播場次不固定、內容少、用戶不穩定的問題。同時可實時或定時自動插入廣告,促進商業變現。
方案概覽
使用播單型導播臺API實現直播場景大致分為4步,如下:
啟用點播系統Bucket:開通視頻點播服務后,不同的服務地域會默認分配一個獨立的存儲Bucket(類型為點播系統Bucket),通過啟用該Bucket,無需其他配置即可進行上傳和媒體資源管理。
上傳音視頻文件:將需要進行直播的視頻文件(例如A、B、C三個視頻文件)按照順序依次上傳至視頻點播(VOD)控制臺存儲。
添加播流域名:觀眾端用于觀看直播的地址。觀眾可以通過播放地址來觀看直播內容。
調用API完成播單型導播臺配置:通過調用播單型導播臺API,對單個視頻的播放次數和播單循環次數分別進行配置。例如文件A播放一遍,文件B播放兩遍,文件C播放三遍。配置完成,查詢整個節目單播放列表信息并開啟播放。
前提條件
您已注冊阿里云賬號并完成賬號實名認證。
依次開通視頻點播服務、視頻直播服務、云導播服務。具體操作,請參見開通視頻點播、開通與購買視頻直播、開通云導播服務。
說明目前導播臺僅在上海(華東2)、北京(華北2)、亞太東南1(新加坡)、印度尼西亞(雅加達)中心開放,后續將會擴展至其他中心。
已準備好播流域名。為了快速體驗,建議直播中心選擇海外且加速區域采用海外及港澳臺加速,此時域名無需備案。
已添加AliyunFCFullAccess、AliyunLiveFullAccess、AliyunVODFullAccess系統權限策略。具體操作,請參見創建RAM用戶并授權。
1.啟用點播系統Bucket
在視頻點播控制臺的配置管理 > 媒資管理配置 > 存儲管理頁面中,單擊點播系統Bucket所在行的啟用。
啟用存儲地址需要一段時間,請您耐心等待。當系統提示存儲地址啟用成功,且存儲地址的狀態為正常后,方可使用該存儲地址。
2.上傳音視頻文件
在點播控制臺的 頁面,單擊上傳音/視頻。
在上傳音/視頻頁面,單擊添加音/視頻。
在添加音/視頻彈框中,選擇本地上傳,將本地準備好的三個視頻文件依次上傳,單擊開始上傳。
成功后,可在音/視頻頁面查看并記錄資源ID。然后單擊操作列的更多 > 用于直播導播,播放內容可以實時在視頻直播的導播臺中關聯顯示。導播臺功能說明,請參見功能區介紹。
3.添加播流域名
在視頻直播控制臺的推/播流域名管理 頁面中,單擊添加域名。
根據實際需求配置域名詳情。單擊下一步。配置參數的具體說明請參見添加加速域名。
記錄生成的CNAME記錄值,供下一步使用。
在云解析DNS控制臺的域名解析頁面,單擊待設置的域名操作列的解析設置。
單擊添加記錄,完成CNAME配置,單擊確認。更多詳細信息,請參見配置域名的CNAME解析。
4.調用API完成播單型導播臺配置
打開Intellij IDEA創建Maven項目,在pom.xml中新建 <dependencies></dependencies>標簽,并添加如下依賴。
<dependencies> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-live</artifactId> <version>3.9.52</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.6.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version> </dependency> </dependencies>
在項目
src/main/java
目錄下,右鍵單擊java,創建Java類。例如命名為bodan。配置如下示例代碼相關參數并運行。
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"; 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(); } } }
參數配置信息說明:
參數配置信息
說明
private final static String ACCESS_KEY_ID = "AccessKey ID"; private final static String ACCESS_KEY_SECRET = "AccessKey Secret";
您需要使用AccessKey完成身份驗證。AccessKey包括AccessKey ID和AccessKey Secret。具體如下:
AccessKey ID:用于標識用戶。
AccessKey Secret:用于驗證用戶的密鑰。AccessKey Secret必須保密。
更多信息,請參見創建AccessKey。
request.setDomainName("example.aliyundoc.com");
播流域名。
String[] resourceIds = new String[]{"698d2b23581f476ea71107703e64****", "9c97e83e211a435b9f797e4e20ee****", "76c6addaa41c438985666a8a964f****"};
資源ID。請參見2.上傳音視頻文件獲取。
查看運行結果。
在云導播臺頁面,可查看已創建的導播臺,單擊操作列的進入,即可看到三個點播文件按照順序依次添加到節目單中,并按照相關配置進行播放。
后續操作
如果需要播放,您可以單擊云導播臺的播放地址并復制,通過VLC播放器、直播Demo或播放器SDK進行播放。具體操作,請參見直播播放。