本文主要是介绍对接萤石平台调用播放,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
萤石api
管理 · 萤石开放平台API文档
httpclient pom
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.2</version></dependency>
package com.ruoyi.common.utils.http;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;public class HttpClientUtil {private HttpClientUtil() {}public static String doPost(String url, Map<String, String > data, Map<String, String >header) throws Exception {// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String resultString = "";try {// 创建Http Post请求HttpPost httpPost = new HttpPost(url);// 创建请求内容httpPost.setHeader("HTTP Method","POST");httpPost.setHeader("Connection","Keep-Alive");for (String key : header.keySet()) {httpPost.setHeader(key,header.get(key));}// 添加参数List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();for (String key : data.keySet()) {nameValuePairs.add(new BasicNameValuePair(key, data.get(key)));}httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));// 执行http请求response = httpClient.execute(httpPost);if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {resultString = EntityUtils.toString(response.getEntity(), "UTF-8");}} catch (Exception e) {throw e;} finally {try {if(response!=null){response.close();}} catch (IOException e) {e.printStackTrace();}}return resultString;}}
根据appKey和secret获取accessToken
-
请求地址
https://open.ys7.com/api/lapp/token/get
-
-
public Map<String, Object> getToken() throws Exception {Map<String, Object> returnMap = redisCache.getCacheObject("redisKey");if (StringUtils.isNotEmpty(returnMap)) {logger.info("获取萤石 token->redis:" +JSON.toJSONString(returnMap)); return returnMap;}Map<String, String> params = Maps.newHashMap();params.put("appKey", AppKey);params.put("appSecret", Secret);String url = https://open.ys7.com/api/lapp/token/get;String json = JSON.toJSONString(params);logger.info("获取萤石 token->params :"+json);Map<String, String > paramHeader = Maps.newHashMap();paramHeader.put("Content-Type", "application/x-www-form-urlencoded");String str = HttpClientUtil.post(url, params, paramHeader);returnMap = JSON.parseObject(str);logger.info("获取萤石token->resDta:"+JSON.toJSONString(returnMap));if (!MapUtils.getString(returnMap, "code").equals("200")) {getToken();}Map<String, Object> info = (Map<String, Object>)returnMap.get("data");redisCache.setCacheObject("redisKey",info ,7, TimeUnit.DAYS);return info;}
播放地址接口
播放地址接口(新接口) · 萤石开放平台API文档
后台获取播放地址
public String getAdress() throws Exception {Map<String, Object> info = getToken();String accessToken = MapUtils.getString(info, "accessToken");Map<String, String > paramHeader = Maps.newHashMap();paramHeader.put("Content-Type", "application/x-www-form-urlencoded");Map<String, String > params = Maps.newHashMap();params.put("accessToken", accessToken);//deviceSerial = 直播源,例如427734222,均采用英文符号params.put("deviceSerial", deviceSerial);logger.info("入参:"+JSON.toJSONString(params));String str =HttpClientUtil.post("https://open.ys7.com/api/lapp/v2/live/address/get", params, paramHeader);logger.info("出参:"+str);Map<String, Object> returnMap = JSON.parseObject(str);Map<String, Object> infoa = (Map<String, Object>)returnMap.get("data");return MapUtils.getString(infoa, "url");}
前端通过地址播放
<iframe src="https://open.ys7.com/jssdk/theme.html?url='+res.url+'&accessToken='+res.accessToken+'&id=ysopen"width="100%"height="600px"id="ysOpenDevice" ></iframe>
这篇关于对接萤石平台调用播放的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!