JavaSDK

最近更新时间:2020-05-07 14:44:12

视频云直播SDK for java 使用指南

SDK下载地址为:https://github.com/KscSDK

安装java sdk

1.git 安装

git clone https://github.com/KscSDK/ksc-sdk-java.git

mvn clean install

2.mvn 中央仓库安装

设置pom文件即可

3.工程使用sdk录

建议使用Maven构建自己的项目,导入ksc-sdk-java,添加ksc-sdk-java-kls的依赖。

新建maven项目
进入到pom文件下,进行如下配置
<dependencies>
    <dependency>
        <groupId>com.ksyun</groupId>
        <artifactId>ksc-sdk-java-kls</artifactId>
        <version>0.3.6</version>
    </dependency>
</dependencies>

4.通过文件配置及管理密钥

本地文件配置:
 ~/.aws/credentials                     Linux,MacOS和Unix系统
C:\Users\{USERNAME}\.aws\credentials    Windows系统
该文件包含下述内容:
[default]
aws_access_key_id = your_acces s_key_id
aws_secret_access_key = your_secret_access_key
如不通过本地文件读入ak和sk信息,则需要在执行的类文件中增加以下代码:
AWSCredentials credentials = new BasicAWSCredentials(aws_access_key_id, aws_secret_access_key);

SDK介绍和使用

在调用接口前,先初始化 KSCOFFJsonClient,如下用例:
KSCKSLClient kls_client = new KSCKSLClient();
//可传入AWSCredentials参数 如:KSCKSLClient kls_client = new KSCKSLClient(credentials);
kls_client.setEndpoint("http://kls.cn-beijing-6.api.ksyun.com/");
字符集:utf-8

SDK介绍和使用

接口目录说明

注:接口的传递的参数及返回值请见<API参考>

1.定时录制接口

  • 接口名: CreateRecordTask

  • 调用方式
   public void testCreateRecordTask() {
        CreateRecordRequest request = new CreateRecordRequest();
        String data = PreparedData(this.startUnixTime,this.endUnixTime);
        request.setData(data);
        CreateRecordResult result =  kls_client.createRecordTask(request);
        System.out.println("===============================");
        log.info(result.getData().getApp());
        System.out.println("================================");
    }

//组织定时录制需要的参数
    private String PreparedData(int startUnixTime,int endUnixTime) {
        JSONObject data = new JSONObject();
        data.put("UniqueName", this.uniquename);
        data.put("App", this.app);
        data.put("Pubdomain", this.pubdomain);
        data.put("Stream", this.stream);
        data.put("StartUnixTime", startUnixTime);
        data.put("EndUnixTime", endUnixTime);
        if (this.mp4VodEnable != 0 ) {
            data.put("Mp4VodEnable", this.mp4VodEnable);
        }

        /**
         * 根据自己业务写入参数,请务必写入必传参数
         * 其余参数请参见https://docs.ksyun.com/documents/1089
         */
        return data.toString();
    }

2.定时录制取消接口

  • 接口名: CancelRecordTask

  • 调用方式
public void testCancelRecord() {
        CancelRecordRequest request = new CancelRecordRequest();
        String data = PreparedCancelRecordData(this.recID);
        request.setData(data);
        CancelRecordResult result = kls_client.cancelRecordTask(request);
        System.out.println("=============================");
        log.info(result.getData());
        System.out.println("=============================");
    }

    //组织取消定时录制任务需要的数据
    private String PreparedCancelRecordData(int recID) {
        JSONObject data = new JSONObject();
        data.put("UniqueName", this.uniquename);
        data.put("App", this.app);
        data.put("Pubdomain", this.pubdomain);
        data.put("Stream", this.stream);
        data.put("RecID", recID);

        /**
         * 以上参数全部为必选参数 https://docs.ksyun.com/documents/1090
         */
        return data.toString();
    }

3.禁止单路直播流推送

  • 接口名: ForbidStream

  • 调用方式
  public void testForbidStream() {
        ForbidStreamRequest request = new ForbidStreamRequest();
        String data = PreparedForbidStreamData();
        request.setData(data);
        ForbidStreamResult result = kls_client.forbidStream(request);
        System.out.println("==============================");
        log.info(result.getData());
        System.out.println("==============================");
    }
// 组织禁流数据
    private String PreparedForbidStreamData() {
        JSONObject data = new JSONObject();
        data.put("UniqueName", this.uniquename);
        data.put("App", this.app);
        data.put("Pubdomain", this.pubdomain);
        data.put("Stream", this.stream);

        /**
         * 以上参数全部为必选参数 https://docs.ksyun.com/documents/1085
         */
        return data.toString();
    }

4.恢复单路流直播推送接口

  • 接口名: ResumeStream

  • 调用方式

    public void testResumeStream() {
        ResumeStreamRequest request = new ResumeStreamRequest();
        String data = PreparedResumeStreamData();
        request.setData(data);
        ResumeStreamResult result = kls_client.resumeStream(request);
        System.out.println("==============================");
        log.info(result.getData());
        System.out.println("==============================");
    }

    //组织恢复禁流的数据
    private String PreparedResumeStreamData() {
        JSONObject data = new JSONObject();
        data.put("UniqueName", this.uniquename);
        data.put("App", this.app);
        data.put("Pubdomain", this.pubdomain);
        data.put("Stream", this.stream);

        /**
         * 以上参数全部为必选参数 https://docs.ksyun.com/documents/1086
         */
        return data.toString();
    }

5.查询黑名单列表接口

  • 接口名: GetBlacklist

  • 调用方式
 public void testGetBlacklist() {
        GetBlacklistRequest request = new GetBlacklistRequest();
        request.setUniqueName(this.uniquename);
        request.setApp(this.app);
        request.setPubdomain(this.pubdomain);
        GetBlacklistResult result = kls_client.getBlacklist(request);
        System.out.println("==============================");
        log.info(result.getData().getApp());
        System.out.println("==============================");
    }

6.检查流是否在黑名单内

  • 接口名: CheckBlacklist

  • 调用方式
public void testCheckBlacklist() {
        CheckBlacklistRequest request = new CheckBlacklistRequest();
        request.setUniqueName(this.uniquename);
        request.setApp(this.app);
        request.setPubdomain(this.pubdomain);
        request.setStream(this.stream);
        CheckBlacklistsResult result = kls_client.checkBlacklist(request);
        System.out.println("==============================");
        log.info(result.getData().getRetMsg());
        System.out.println("==============================");
    }

7.短视频开始录制接口

  • 接口名: StartStreamRecord

  • 调用方式

   public void testStartStreamRecord() {
        StartStreamRecordRequest request = new StartStreamRecordRequest();
        String data = PreparedStartStreamRecordData();
        request.setData(data);
        StartStreamRecordResult result = kls_client.startStreamRecord(request);
        System.out.println("==============================");
        log.info(result.getData());
        System.out.println("==============================");
    }

 //组织开始录制需要的数据
    private String PreparedStartStreamRecordData() {
        JSONObject data = new JSONObject();
        data.put("UniqueName", this.uniquename);
        data.put("App", this.app);
        data.put("Pubdomain", this.pubdomain);
        data.put("Stream", this.stream);

        /**
         * 以上参数全部为必选参数
         * 请根据业务需求 写入可选参数 参见https://docs.ksyun.com/documents/1091
         */
        return data.toString();
    }

8.短视频结束录制接口

  • 接口名: StopStreamRecord

  • 调用方式
public void testStopStreamRecord() {
        StopStreamRecordRequest request = new StopStreamRecordRequest();
        String data = PreparedStopStreamRecordData();
        request.setData(data);
        StopStreamRecordResult result = kls_client.stopStreamRecord(request);
        System.out.println("==============================");
        log.info(result.getData());
        System.out.println("==============================");
    }

    //组织结束录制任务需要的数据
    private String PreparedStopStreamRecordData() {
        JSONObject data = new JSONObject();
        data.put("UniqueName", this.uniquename);
        data.put("App", this.app);
        data.put("Pubdomain", this.pubdomain);
        data.put("Stream", this.stream);
        data.put("RecID", recID);

        /**
         * 以上参数全部为必选参数 https://docs.ksyun.com/documents/1092
         */
        return data.toString();
    }

9.查询在线录制任务接口

  • 接口名: ListRecordingTasks

  • 调用方式
public void testListRecordingTasks() {
        ListRecordingTasksRequest request = new ListRecordingTasksRequest();
        request.setUniqueName(this.uniquename);
        request.setApp(this.app);
        request.setPubdomain(this.pubdomain);
        ListRecordingTasksResult result = kls_client.listRecordingTasks(request);
        System.out.println("==============================");
        log.info(result.getData());
        System.out.println("==============================");
    }

10.查询历史录制任务接口

  • 接口名: ListHistoryRecord

  • 调用方式
 public void testListHistoryRecordTasks() {
        ListHistoryRecordTasksRequest request = new ListHistoryRecordTasksRequest();
        request.setApp(this.app);
        request.setPubdomain(this.pubdomain);
        request.setUniqueName(this.uniquename);
        ListHistoryRecordTasksResult result = kls_client.listHistoryRecordTasks(request);
        System.out.println("==============================");
        log.info(result.getData());
        System.out.println("==============================");
    }

11.查询录像任务状态接口

  • 接口名: GetRecordTask

  • 调用方式
     public void testGetRecordTask() {
        GetRecordTaskRequest request = new GetRecordTaskRequest();
        request.setAction(this.action);
        request.setVersion(this.version);
        request.setRecID(this.recID);
        GetRecordTaskResult result = kls_client.getRecordTask(request);
        System.out.println("==============================");
        log.info(result.getData());
        System.out.println("==============================");
    }

12.查询主播流时长接口

  • 接口名: ListStreamDurations

  • 调用方式
    public void testListStreamDurations() {
        ListStreamDurationsRequest request = new ListStreamDurationsRequest();
        request.setAction(this.action);
        request.setVersion(this.version);
        request.setUniqueName(this.uniquename);
        request.setApp(this.app);
        request.setPubdomain(this.pubdomain);
        request.setStream(this.stream);
        request.setStartUnixTime(this.startUnixTime);
        request.setEndUnixTime(this.endUnixTime);
        ListStreamDurationsResult result = kls_client.listStreamDurations(request);
        System.out.println("==============================");
        log.info(result.getData());
        System.out.println("==============================");
    }

13.查询流历史信息接口

  • 接口名: ListHistoryPubStreamsInfo

  • 调用方式
   public void testListHistoryPubStreamsInfo() {
        ListHistoryPubStreamsInfoRequest request = new ListHistoryPubStreamsInfoRequest();
        request.setUniqueName(this.uniquename);
        request.setApp(this.app);
        request.setPubdomain(this.pubdomain);
        ListHistoryPubStreamsInfoResult result = kls_client.listHistoryPubStreamsInfo(request);
        System.out.println("==============================");
        log.info(result.getData());
        System.out.println("==============================");
    }

14.查询流历史错误信息接口

  • 接口名: ListHistoryPubStreamsErrInfo

  • 调用方式
    public void testListHistoryPubStreamsErrInfo() {
        ListHistoryPubStreamsErrInfoRequest request = new ListHistoryPubStreamsErrInfoRequest();
        request.setUniqueName(this.uniquename);
        request.setApp(this.app);
        request.setPubdomain(this.pubdomain);
        ListHistoryPubStreamsErrInfoResult result = kls_client.listHistoryPubStreamsErrInfo(request);
        System.out.println("==============================");
        log.info(result.getData());
        System.out.println("==============================");
    }

15.查询主播推拉流实时信息接口

  • 接口名: ListRealtimeStreamsInfo

  • 调用方式
    public void testListRealtimeStreamsInfo() {
        ListRealtimeStreamsInfoRequest request = new ListRealtimeStreamsInfoRequest();
        String data = PreparedListRealtimeStreamsInfoData();
        request.setData(data);
        ListRealtimeStreamsInfoResult result = kls_client.listRealtimeStreamsInfo(request);
        System.out.println("==============================");
        log.info(result.getData());
        log.info(result.getData().getApp());
        System.out.println("============Bw==================");
        log.info(result.getData().getBw());
        System.out.println("============OnlineUser==================");
        log.info(result.getData().getOnlineUser());
        System.out.println("============Streams 第一条的BW信息==================");
        log.info(result.getData().getStreams().get(0).getBw());
        System.out.println("============Streams clientIP==================");
        log.info(result.getData().getStreams().get(0).getClientip());
        System.out.println("============Streams pubtime==================");
        log.info(result.getData().getStreams().get(0).getPubtime());
        System.out.println("============Streams framerate==================");
        log.info(result.getData().getStreams().get(0).getFrameRate());
        System.out.println("============返回参数==================");
        log.info(result.getData().getRetCode());
        log.info(result.getData().getRetMsg());
        System.out.println("==============================");
    }

    //组织查询主播推拉流实时信息需要的数据
    private String PreparedListRealtimeStreamsInfoData() {
        JSONObject data = new JSONObject();
        data.put("UniqueName", this.uniquename);
        data.put("App", this.app);
        /**
         * 以上参数全部为必选参数 https://docs.ksyun.com/documents/1080
         */
        return data.toString();
    }

16.查询推流实时信息接口

  • 接口名: ListRealtimePubStreamsInfo

  • 调用方式
    public void testListRealtimePubStreamsInfo() {
        ListRealtimePubStreamsInfoRequest request = new ListRealtimePubStreamsInfoRequest();
        request.setUniqueName(this.uniquename);
        request.setApp(this.app);
        request.setPubdomain(this.pubdomain);
        ListRealtimePubStreamsInfoResult result = kls_client.listRealtimePubStreamsInfo(request);
        System.out.println("==============================");
        log.info(result.getData());
        System.out.println("==============================");
    }

金山云,开启您的云计算之旅

免费注册