开发者社区> 问答> 正文

如何使用 JAVA SDK 生成推流地址?

如何使用 JAVA SDK 生成推流地址

展开
收起
1358896759097293 2021-03-10 20:26:06 1080 0
1 条回答
写回答
取消 提交回答
  • 45271990@qq.com

    使用 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());
     }
     }
    
    
    2021-03-12 23:17:58
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
跨平台的云服务SDK需要什么 立即下载
如何通过 Serverless 提高 Java 微服务治理效 立即下载
一个跨平台的云服务SDK需要什么 立即下载