本文介绍视频直播服务端Java SDK的使用方法和示例代码。以查询直播截图配置接口为例,帮助您快速掌握视频直播接口的使用方法。
前提条件
调用服务端接口需要使用AccessKey完成身份验证,请提前获取AccessKey。获取方法请参见创建AccessKey。
已下载服务端Java SDK,详细信息,请参见SDK下载。
操作步骤
修改服务端SDK(jar包)中的pom.xml文件,引入阿里云核心SDK和视频直播SDK。此处版本仅供参考,获取最新的版本请参见视频直播SDK。
引入阿里云核心SDK。
<dependencies> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.4.6</version> </dependency> </dependencies>
引入视频直播SDK。
<dependencies> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-live</artifactId> <version>3.9.0</version> </dependency> </dependencies>
初始化IAcsClient实例。
您在工程中引入配置需要使用Spring框架,请参见搭建Spring项目。
在配置文件中配置:
# 客户端请求的ak、sk,就是应用的密钥信息 live.accessKeyId=<yourAccessKeyId> live.accessKeySecret=<yourAccessKeySecret>
视频直播SDK通过IAcsClient实例完成OpenAPI的调用。因此,在发起调用前需要先初始化IAcsClient实例。
@Value("${live.accessKeyId}") private String accessKeyId; @Value("${live.accessKeySecret}") private String accessKeySecret; DefaultAcsClient client; @Before public void initClient() throws Exception { // 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 // 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。 // 本示例通过从配置文件中读取AccessKey,来实现API访问的身份验证。 DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret); client = new DefaultAcsClient(profile); }
初始化请求。
调用API前,需先初始化对应的请求实例,此处以DescribeLiveSnapshotConfig接口为例,示例代码如下所示:
public void requestInitSample() { DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest(); describeLiveSnapshotConfigRequest.setDomainName("example.aliyundoc.com"); //describeLiveSnapshotConfigRequest.setProtocol(ProtocolType.HTTPS); //指定访问协议 //describeLiveSnapshotConfigRequest.setAcceptFormat(FormatType.JSON); //指定API返回格式 //describeLiveSnapshotConfigRequest.setMethod(MethodType.POST); //指定请求方法 //describeLiveSnapshotConfigRequest.setRegionId("cn-shanghai");//指定要访问的Region,仅对当前请求生效,不改变client的默认设置 try { HttpResponse httpResponse = client.doAction(describeLiveSnapshotConfigRequest); System.out.println(httpResponse.getUrl()); System.out.println(new String(httpResponse.getContent())); //todo something } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } } }
调用OpenAPI并解析结果。
IAcsClient提供了两种类型的调用结果,如下所示:
调用
doAction
方法获取原始的API调用结果,即返回HttpResponse
类型的结果。示例代码如下所示:public void invokeSample() { DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest(); describeLiveSnapshotConfigRequest.setDomainName("example.aliyundoc.com"); try { HttpResponse httpResponse = client.doAction(describeLiveSnapshotConfigRequest); System.out.println(httpResponse.getUrl()); System.out.println(new String(httpResponse.getContent())); //todo something else } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } }
返回结果如下所示:
当http status大于等于200且小于300时,表示API调用成功。
当http status大于等于300且小于500时,服务端SDK会提示ClientException,表示客户端错误。
当http status大于等于500时,服务端SDK会提示ServerException,表示服务器端错误。
调用
getAcsResponse
方法,获取反序列化后的对象,示例代码如下所示:public void invokeSample() { DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest(); describeLiveSnapshotConfigRequest.setDomainName("example.aliyundoc.com"); try { DescribeLiveSnapshotConfigResponse describeLiveSnapshotConfigResponse = client.getAcsResponse(describeLiveSnapshotConfigRequest); //todo something } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } }