简单介绍一下,阿里云的播流推流地址的实现方式。
相关文档路径:https://help.aliyun.com/document_detail/85018.html?spm=5176.11202509.0.0.7df22699DqAKUE
最重要的是推流与播流的鉴权Key(是两种key,也可自定义设置成一致的)。在阿里云地址生成界面,选择播流域名,点击修改即可查看到相对应的鉴权Key。
拿到鉴权KEY后,就可以直接撸代码了。
DigestUtils.md5Hex,此方法 是使用Apache的(无需网上抄),md5Hex 加密成32位。
public static void demo(){
//1.AppName 与 StreamName (自定义)
String appName = "timestamp";
String streamName = "streamName";
//2.失效时间 timestamp 10位数的时间戳
Long time = System.currentTimeMillis();
time += 30 * 1000 * 60; //当前时间加上30分钟
String timestamp = String.valueOf(new Timestamp(new Date(time).getTime()).getTime()/1000);
//3.鉴权的Key :a.播流鉴权Key b.推流鉴权Key (来自于阿里云视频直播设置)
String pushKey = "1234abcd"; //在此处,我是随意写的,实际需要与阿里云中所配置的一致
String playKey = "5678qwer";
//4.需要加密的字符串 有关 rang 可以是UUID,在此我默认为0
String strPush = "/" + appName + "/" + appName + "-" + timestamp + "-" + "0-0-" + pushKey;//推流
String strPlay = "/" + appName + "/" + appName + "-" + timestamp + "-" + "0-0-" + playKey;//播流(rtmp类型,其他类型需要加密的字符不一致)
//5.pushHost 推流域名 playHost 播流域名
String pushHost = "push.*****.com"; // 实际地址在阿里云配置
String playHost = "live.*****.com"; // 实际地址在阿里云配置
//6.生产 推流地址
String pushUrl = "rtmp://" + pushHost + "/" + appName + "/" + streamName + "?auth_key=" + timestamp + "-0-0-" + DigestUtils.md5Hex(strPush);
//7.产生播流地址 (rtmp,在此就展示一种!)
String playUrl = "rtmp://" + playHost + "/" + appName + "/" + streamName + "?auth_key=" + timestamp + "-0-0-" + DigestUtils.md5Hex(strPlay);
}