本文介绍Java SDK的LiveChannel常见操作,如创建LiveChannel、列举LiveChannel及删除LiveChannel等。
说明
本文示例由阿里云用户bin提供,仅供参考。
创建LiveChannel
通过RTMP协议上传音视频数据前,必须先调用该接口创建一个LiveChannel。调用PutLiveChannel接口会返回RTMP推流地址,以及对应的播放地址。
说明 您可以使用返回的地址进行推流、播放,您还可以根据该LiveChannel的名称来发起相关的操作,如查询推流状态、查询推流记录、禁止推流等。
以下代码用于创建LiveChannel。
public static void createLiveChannel() {
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// 填写LiveChannel名称。
String liveChannelName = "yourLiveChannelName";
// 创建OSSClient实例。
OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
CreateLiveChannelRequest request = new CreateLiveChannelRequest(bucketName,
liveChannelName, "desc", LiveChannelStatus.Enabled, new LiveChannelTarget());
CreateLiveChannelResult result = oss.createLiveChannel(request);
//获取推流地址。
List<String> publishUrls = result.getPublishUrls();
for (String item : publishUrls) {
System.out.println(item);
}
//获取播放地址。
List<String> playUrls = result.getPlayUrls();
for (String item : playUrls) {
System.out.println(item);
}
oss.shutdown();
}
列举LiveChannel
以下代码用于列举指定的LiveChannel。
public static void listLiveChannels() {
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// 填写Bucket名称。
String bucketName = "yourBucketName";
// 创建OSSClient实例。
OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ListLiveChannelsRequest request = new ListLiveChannelsRequest(bucketName);
LiveChannelListing liveChannelListing = oss.listLiveChannels(request);
System.out.println(JSON.toJSONString(liveChannelListing));
oss.shutdown();
}
删除LiveChannel
说明
- 当有客户端正在向LiveChannel推流时,删除请求会失败。
- DeleteLiveChannel接口只会删除LiveChannel本身,不会删除推流生成的文件。
以下代码用于删除指定的LiveChannel。
public static void deleteLiveChannel() {
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// 填写Bucket名称。
String bucketName = "yourBucketName";
// 创建OSSClient实例。
OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
LiveChannelGenericRequest request = new LiveChannelGenericRequest(bucketName, liveChannelName);
try {
oss.deleteLiveChannel(request);
} catch (OSSException ex) {
ex.printStackTrace();
} catch (ClientException ex) {
ex.printStackTrace();
} finally {
oss.shutdown();
}
}
设置LiveChannel状态
LiveChannel有enabled和disabled两种状态供您选择。
以下代码用于设置LiveChannel状态。
public static void setLiveChannelStatus() {
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// 填写LiveChannel名称。
String liveChannelName = "yourLiveChannelName";
// 填写Bucket名称。
String bucketName = "yourBucketName";
// 创建OSSClient实例。
OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
oss.setLiveChannelStatus(bucketName, liveChannelName, LiveChannelStatus.Enabled);
} catch (OSSException ex) {
System.out.println(ex.getErrorCode().concat(",").concat(ex.getErrorMessage()));
} catch (ClientException ex) {
System.out.println(ex.getErrorCode().concat(",").concat(ex.getErrorMessage()));
} finally {
oss.shutdown();
}
}
获取LiveChannel鉴权地址
以下代码用于获取LiveChannel鉴权地址。
public static void createLiveChannel() {
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// 填写LiveChannel名称。
String liveChannelName = "yourLiveChannelName";
// 填写Bucket名称。
String bucketName = "yourBucketName";
// 创建OSSClient实例。
OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
CreateLiveChannelRequest request = new CreateLiveChannelRequest(bucketName,
liveChannelName, "desc", LiveChannelStatus.Enabled, new LiveChannelTarget());
CreateLiveChannelResult result = oss.createLiveChannel(request);
// 获取推流地址。
List<String> publishUrls = result.getPublishUrls();
for (String item : publishUrls) {
// 获取不包含签名信息的推流地址。
System.out.println(item);
// 获取包含签名信息的推流地址。
LiveChannelInfo liveChannelInfo = oss.getLiveChannelInfo(bucketName, liveChannelName);
// expires表示过期时间,单位为Unix时间戳。本示例以设置过期时间为1小时为例。
long expires = System.currentTimeMillis() / 1000 + 3600;
// playlistName表示调用createLiveChannel接口时传入的具体名称。如果未传入该参数,则默认值为"playlist.m3u8"。您也可以通过调用getLiveChannelInfo接口获取该名称。
String signRtmpUrl = oss.generateRtmpUri(bucketName, liveChannelName, liveChannelInfo.getTarget().getPlaylistName(), expires);
System.out.println(signRtmpUrl);
}
// 获取播放地址。
List<String> playUrls = result.getPlayUrls();
for (String item : playUrls) {
System.out.println(item);
}
oss.shutdown();
}
获取LiveChannel状态信息
以下代码用于获取指定LiveChannel的推流状态信息。
public static void getLiveChannelStat() {
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// 填写LiveChannel名称。
String liveChannelName = "yourLiveChannelName";
// 填写Bucket名称。
String bucketName = "yourBucketName";
// 创建OSSClient实例。
OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
LiveChannelStat liveChannelStat = oss.getLiveChannelStat(bucketName, liveChannelName);
System.out.println(liveChannelStat.toString());
oss.shutdown();
}
获取LiveChannel配置信息
以下代码用于获取指定LiveChannel 的配置信息。
public static void getLiveChannelInfo() {
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// 填写LiveChannel名称。
String liveChannelName = "yourLiveChannelName";
// 填写Bucket名称。
String bucketName = "yourBucketName";
// 创建OSSClient实例。
OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
LiveChannelInfo liveChannelInfo = oss.getLiveChannelInfo(bucketName, liveChannelName);
System.out.println(JSON.toJSONString(liveChannelInfo));
oss.shutdown();
}
生成LiveChannel播放列表
PostVodPlaylist接口用于为指定的LiveChannel生成一个点播用的播放列表。OSS会查询指定时间范围内由该LiveChannel推流生成的ts文件,并将其拼装为一个m3u8播放列表。
以下代码用于生成LiveChannel播放列表。
public static void postVodPlaylist() {
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// 填写LiveChannel名称。
String liveChannelName = "yourLiveChannelName";
// 填写Bucket名称。
String bucketName = "yourBucketName";
// 填写PlayList名称。
String playListName = "yourPlayListName";
// 创建OSSClient实例。
OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
long startTime = getUnixTimestamp("2019-06-27 23:00:00");
long endTIme = getUnixTimestamp("2019-06-28 22:00:00");
try {
oss.generateVodPlaylist(bucketName, liveChannelName, playListName, startTime, endTIme);
} catch (OSSException ex) {
System.out.println(ex.getErrorCode().concat(",").concat(ex.getErrorMessage()));
} catch (ClientException ex) {
System.out.println(ex.getErrorCode().concat(",").concat(ex.getErrorMessage()));
} finally {
oss.shutdown();
}
}
private static long getUnixTimestamp(String time) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = format.parse(time);
return date.getTime() / 1000;
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
查看LiveChannel播放列表
以下代码用于查看指定LiveChannel推流生成的且满足指定时间段内的播放列表。
public static void getVodPlaylist() {
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// 填写LiveChannel名称。
String liveChannelName = "yourLiveChannelName";
// 填写Bucket名称。
String bucketName = "yourBucketName";
// 创建OSSClient实例。
OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
long startTime = getUnixTimestamp("2019-06-27 23:00:00");
long endTIme = getUnixTimestamp("2019-06-28 22:00:00");
try {
OSSObject ossObject = oss.getVodPlaylist(bucketName, liveChannelName, startTime, endTIme);
System.out.println(ossObject.toString());
} catch (OSSException ex) {
System.out.println(ex.getErrorCode().concat(",").concat(ex.getErrorMessage()));
} catch (ClientException ex) {
System.out.println(ex.getErrorCode().concat(",").concat(ex.getErrorMessage()));
} finally {
oss.shutdown();
}
}
private static long getUnixTimestamp(String time) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = format.parse(time);
return date.getTime() / 1000;
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
获取LiveChannel推流记录
GetLiveChannelHistory接口用于获取指定LiveChannel的推流记录。使用GetLiveChannelHistory接口最多会返回指定LiveChannel最近的10次推流记录。
以下代码用于获取LiveChannel推流记录。
public void getLiveChannelHistory() {
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// 填写LiveChannel名称。
String liveChannelName = "yourLiveChannelName";
// 填写Bucket名称。
String bucketName = "yourBucketName";
// 创建OSSClient实例。
OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
List<LiveRecord> list = oss.getLiveChannelHistory(bucketName, liveChannelName);
System.out.println(JSON.toJSONString(list));
oss.shutdown();
}
相关文档
- 关于创建LiveChannel的API接口说明,请参见PutLiveChannel。
- 关于列举LiveChannel的API接口说明,请参见ListLiveChannel。
- 关于删除LiveChannel的API接口说明,请参见DeleteLiveChannel。
- 关于设置LiveChannel的API接口说明,请参见PutLiveChannelStatus。
- 关于获取LiveChannel的API接口说明,请参见GetLiveChannelStat。
- 关于获取LiveChannel配置信息的API接口说明,请参见GetLiveChannelInfo。
- 关于生成LiveChannel播放列表的API接口说明,请参见PostVodPlaylist。
- 关于查看LiveChannel播放列表的API接口说明,请参见GetVodPlaylist。
- 关于获取LiveChannel推流记录的API接口说明,请参见GetLiveChannelHistory。