本文介绍 Java SDK 的 LiveChannel 常见操作,如创建 LiveChannel、列举 LiveChannel 及删除 LiveChannel 等。

说明

本文示例由阿里云用户 bin 提供,仅供参考。

创建 LiveChannel

通过 RTMP 协议上传音视频数据前,必须先调用该接口创建一个 LiveChannel。调用 PutLiveChannel 接口会返回 RTMP 推流地址,以及对应的播放地址。

说明 您可以使用返回的地址进行推流、播放,您还可以根据该 LiveChannel 的名称来发起相关的操作,如查询推流状态、查询推流记录、禁止推流等。

以下代码用于创建 LiveChannel:

public static void createLiveChannel() {
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云主账号 AccessKey 拥有所有 API 的访问权限,风险很高。
        // 强烈建议您创建并使用 RAM 账号进行 API 访问或日常运维,请登录 https://ram.console.aliyun.com 创建 RAM 账号。
        String accessKeyId = "<yourAccessKeyId>";
        String accessKeySecret = "<yourAccessKeySecret>";
        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 详情,请参见PutLiveChannel

列举 LiveChannel

以下代码用于列举指定的 LiveChannel:

public static void listLiveChannels() {
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云主账号 AccessKey 拥有所有 API 的访问权限,风险很高。
        // 强烈建议您创建并使用 RAM 账号进行 API 访问或日常运维,请登录 https://ram.console.aliyun.com 创建 RAM 账号。
        String accessKeyId = "<yourAccessKeyId>";
        String accessKeySecret = "<yourAccessKeySecret>";
        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 详情,请参见ListLiveChannel

删除 LiveChannel

说明
  • 当有客户端正在向 LiveChannel 推流时,删除请求会失败。
  • DeleteLiveChannel 接口只会删除 LiveChannel 本身,不会删除推流生成的文件。

以下代码用于删除指定的 LiveChannel:

public static void deleteLiveChannel() {
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云主账号 AccessKey 拥有所有 API 的访问权限,风险很高。
        // 强烈建议您创建并使用 RAM 账号进行 API 访问或日常运维,请登录 https://ram.console.aliyun.com 创建 RAM 账号。
        String accessKeyId = "<yourAccessKeyId>";
        String accessKeySecret = "<yourAccessKeySecret>";
        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 详情,请参见DeleteLiveChannel

设置 LiveChannel 状态

LiveChannel 有 enabled 和 disabled 两种状态供您选择。

以下代码用于设置 LiveChannel 状态:

public static void setLiveChannelStatus() {
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云主账号 AccessKey 拥有所有 API 的访问权限,风险很高。
        // 强烈建议您创建并使用 RAM 账号进行 API 访问或日常运维,请登录 https://ram.console.aliyun.com 创建 RAM 账号。
        String accessKeyId = "<yourAccessKeyId>";
        String accessKeySecret = "<yourAccessKeySecret>";
        String liveChannelName = "<yourLiveChannelName>";
        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 状态详情,请参见PutLiveChannelStatus

获取 LiveChannel 状态信息

以下代码用于获取指定 LiveChannel 的推流状态信息。

public static void getLiveChannelStat() {
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云主账号 AccessKey 拥有所有 API 的访问权限,风险很高。
        // 强烈建议您创建并使用 RAM 账号进行 API 访问或日常运维,请登录 https://ram.console.aliyun.com 创建 RAM 账号。
        String accessKeyId = "<yourAccessKeyId>";
        String accessKeySecret = "<yourAccessKeySecret>";
        String liveChannelName = "<yourLiveChannelName>";
        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 状态信息详情,请参见GetLiveChannelStat

获取 LiveChannel 配置信息

以下代码用于获取指定 LiveChannel 的配置信息:

public static void getLiveChannelInfo() {
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云主账号 AccessKey 拥有所有 API 的访问权限,风险很高。
        // 强烈建议您创建并使用 RAM 账号进行 API 访问或日常运维,请登录 https://ram.console.aliyun.com 创建 RAM 账号。
        String accessKeyId = "<yourAccessKeyId>";
        String accessKeySecret = "<yourAccessKeySecret>";
        String bucketName = "<yourBucketName>";
        String liveChannelName = "<yourLiveChannelName>";

        // 创建 OSSClient 实例。
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        LiveChannelInfo liveChannelInfo = oss.getLiveChannelInfo(bucketName, liveChannelName);
        System.out.println(JSON.toJSONString(liveChannelInfo));
        oss.shutdown();
    }

获取 LiveChannel 配置信息详情,请参见GetLiveChannelInfo

生成 LiveChannel 播放列表

PostVodPlaylist 接口用于为指定的 LiveChannel 生成一个点播用的播放列表。OSS 会查询指定时间范围内由该 LiveChannel 推流生成的 ts 文件,并将其拼装为一个 m3u8 播放列表。

以下代码用于生成 LiveChannel 播放列表:

public static void postVodPlaylist() {
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云主账号 AccessKey 拥有所有 API 的访问权限,风险很高。
        // 强烈建议您创建并使用 RAM 账号进行 API 访问或日常运维,请登录 https://ram.console.aliyun.com 创建 RAM 账号。
        String accessKeyId = "<yourAccessKeyId>";
        String accessKeySecret = "<yourAccessKeySecret>";
        String liveChannelName = "<yourLiveChannelName>";
        String bucketName = "<yourBucketName>";
        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 播放列表详情,请参见PostVodPlaylist

查看 LiveChannel 播放列表

以下代码用于查看指定 LiveChannel 推流生成的、且指定时间段内的播放列表:

public static void getVodPlaylist() {
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云主账号 AccessKey 拥有所有 API 的访问权限,风险很高。
        // 强烈建议您创建并使用 RAM 账号进行 API 访问或日常运维,请登录 https://ram.console.aliyun.com 创建 RAM 账号。
        String accessKeyId = "<yourAccessKeyId>";
        String accessKeySecret = "<yourAccessKeySecret>";
        String liveChannelName = "<yourLiveChannelName>";
        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 播放列表详情,请参见GetVodPlaylist

获取 LiveChannel 推流记录

GetLiveChannelHistory 接口用于获取指定 LiveChannel 的推流记录。使用 GetLiveChannelHistory 接口最多会返回指定 LiveChannel 最近的 10 次推流记录。

以下代码用于获取 LiveChannel 推流记录:

public void getLiveChannelHistory() {
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云主账号 AccessKey 拥有所有 API 的访问权限,风险很高。
        // 强烈建议您创建并使用 RAM 账号进行 API 访问或日常运维,请登录 https://ram.console.aliyun.com 创建 RAM 账号。
        String accessKeyId = "<yourAccessKeyId>";
        String accessKeySecret = "<yourAccessKeySecret>";
        String bucketName = "<yourBucketName>";
        String liveChannelName = "<yourLiveChannelName>";

        // 创建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 推流记录详情,请参见GetLiveChannelHistory