使用 JAVA SDK 生成推流地址 我们现在用 java 的 SDK 演示一下如上的推理过程,在跑 SDK 之前,需要先搭 建好一套本地的 eclipse 环境,如下是我用的 eclipse,如果有没搭建请网上搜索一 下 eclipse 的搭建方式(之前需要安装 JDK 且配置环境变量)。 环境要求 ● Eclipse 版本:Version: Neon.3 Release (4.6.3)。 ● JDK 版本:jdk1.8.0_144。 ● OSS:公开读(为了验证推流功能是否正常,我们用公开读的方式快速测试)。 ● 我们采用主函数入口的方式,实例化其他类进行调用,这样方便类的拆分,不 用都集合在主函数中。 ● 主函数 domain,实例化 OSSClient 对象传入到 RtmpTest 类中测试。 ● 所有的 jar 都会通过官方的 maven 解决的依赖关系,https://help.aliyun.
com/document_detail/32009.html
package javasdk;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import com.aliyun.oss.OSSClient;
public class domain {
public static void main( String[] args ) throws ParseException,
FileNotFoundException
{
System.out.println( "Hello World!" );
String accessid = "AK";
String secretkey = "SK";
String objectpath = "C://Users//hanli.zyb//Desktop//running.png";
String bucket = "bucket";
String object = "running";
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// OSS + rtmp 推流
String bucketName = "ali-hangzhou";
RtmpTest pushoss = new RtmpTest();
OSSClient ossClient = new OSSClient(endpoint, AK, SK);
pushoss.testCreateLiveChannel(bucketName,ossClient);
}
}
package javasdk;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import junit.framework.Assert;
import org.junit.Ignore;
import org.junit.Test;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSErrorCode;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.utils.DateUtil;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateLiveChannelRequest;
import com.aliyun.oss.model.CreateLiveChannelResult;
import com.aliyun.oss.model.ListLiveChannelsRequest;
import com.aliyun.oss.model.LiveChannel;
import com.aliyun.oss.model.LiveChannelInfo;
import com.aliyun.oss.model.LiveChannelListing;
import com.aliyun.oss.model.LiveChannelStat;
import com.aliyun.oss.model.LiveChannelStatus;
import com.aliyun.oss.model.LiveChannelTarget;
import com.aliyun.oss.model.LiveRecord;
import com.aliyun.oss.model.PushflowStatus;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class RtmpTest {
String bucketName = "bucket";
final String liveChannel = "stream name";
@Test
public void testCreateLiveChannelDefault(String bucketname,OSSClient
ossClient) {
try {
CreateLiveChannelRequest createLiveChannelRequest = new
CreateLiveChannelRequest(
bucketName, liveChannel);
CreateLiveChannelResult createLiveChannelResult = ossClient.
createLiveChannel(createLiveChannelRequest);
LiveChannelInfo liveChannelInfo = ossClient.
getLiveChannelInfo(bucketName, liveChannel);
ossClient.deleteLiveChannel(bucketName, liveChannel);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
@Test
public void testCreateLiveChannel(String bucketname,OSSClient ossClient)
{
final String liveChannel = "normal-create-live-channel";
final String liveChannelDesc = "my test live channel";
try {
LiveChannelTarget target = new LiveChannelTarget("HLS", 100, 99,
"myplaylist.m3u8");
CreateLiveChannelRequest createLiveChannelRequest = new
CreateLiveChannelRequest(
bucketName, liveChannel, liveChannelDesc,
LiveChannelStatus.Enabled, target);
CreateLiveChannelResult createLiveChannelResult = ossClient.
createLiveChannel(createLiveChannelRequest);
System.out.println(createLiveChannelResult.getPublishUrls());
/*Assert.assertEquals(createLiveChannelResult.getPublishUrls().
size(), 1);
Assert.assertTrue(createLiveChannelResult.getPublishUrls().
get(0).startsWith("rtmp://"));
Assert.assertTrue(createLiveChannelResult.getPublishUrls().
get(0).endsWith("live/" + liveChannel));
Assert.assertEquals(createLiveChannelResult.getPlayUrls().size(),
1);
Assert.assertTrue(createLiveChannelResult.getPlayUrls().get(0).
startsWith("http://"));
Assert.assertTrue(createLiveChannelResult.getPlayUrls().get(0).
endsWith(liveChannel + "/myplaylist.m3u8"));*/
/* LiveChannelInfo liveChannelInfo = ossClient.
getLiveChannelInfo(bucketName, liveChannel);
Assert.assertEquals(liveChannelInfo.getDescription(),
liveChannelDesc);
Assert.assertEquals(liveChannelInfo.getStatus(),
LiveChannelStatus.Disabled);
Assert.assertEquals(liveChannelInfo.getTarget().getType(),
"HLS");
Assert.assertEquals(liveChannelInfo.getTarget().
getFragDuration(), 100);
Assert.assertEquals(liveChannelInfo.getTarget().getFragCount(),
99);
Assert.assertEquals(liveChannelInfo.getTarget().
getPlaylistName(), "myplaylist.m3u8");*/
// ossClient.deleteLiveChannel(bucketName, liveChannel);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
}
其中我们最关注的是 testCreateLiveChannel 类,创建了推流地址,其中参数 LiveChannelStatus.enable 是要让推流变为可用状态。running 主函数,打印出 推流地址。rtmp://hangzhou.oss-cn-hangzhou.aliyuncs.com/live/normal-create-live-channel。
public void testCreateLiveChannel(String bucketname,OSSClient ossClient) {
final String liveChannel = "normal-create-live-channel";
final String liveChannelDesc = "my test live channel";
try {
LiveChannelTarget target = new LiveChannelTarget("HLS", 100, 99,
"myplaylist.m3u8");
CreateLiveChannelRequest createLiveChannelRequest = new
CreateLiveChannelRequest(
bucketName, liveChannel, liveChannelDesc,
LiveChannelStatus.Enabled, target);
CreateLiveChannelResult createLiveChannelResult = ossClient.
createLiveChannel(createLiveChannelRequest);
System.out.println(createLiveChannelResult.getPublishUrls());
/*Assert.assertEquals(createLiveChannelResult.getPublishUrls().
size(), 1);
Assert.assertTrue(createLiveChannelResult.getPublishUrls().
get(0).startsWith("rtmp://"));
Assert.assertTrue(createLiveChannelResult.getPublishUrls().
get(0).endsWith("live/" + liveChannel));
Assert.assertEquals(createLiveChannelResult.getPlayUrls().size(),
1);
Assert.assertTrue(createLiveChannelResult.getPlayUrls().get(0).
startsWith("http://"));
Assert.assertTrue(createLiveChannelResult.getPlayUrls().get(0).
endsWith(liveChannel + "/myplaylist.m3u8"));*/
// ossClient.deleteLiveChannel(bucketName, liveChannel);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。