|
Unity 萤石云 监控 Http 云台
控制器
导入sdk 包,加入眼睛 控制器: Unity导航栏中即会出现Tools工具,子级出现ShadowCreator--Action
http插件
资料
新版的SDK,可是确保起见,我用的依旧是旧版mem
1.关于新版本的sdk
2. 云台控制http请求官方文档
请求接口播放地址
"liveAddress": //HLS流畅直播地址"http://hls.open.ys7.com/openlive/ed98eeead4c843f898fef1c203313364.m3u8", "hdAddress": //HLS高清直播地址"http://hls.open.ys7.com/openlive/ed98eeead4c843f898fef1c203313364.hd.m3u8", "rtmp": //RTMP流畅直播地址"rtmp://rtmp.open.ys7.com/openlive/ed98eeead4c843f898fef1c203313364", "rtmpHd": // RTMP高清直播地址"rtmp://rtmp.open.ys7.com/openlive/ed98eeead4c843f898fef1c203313364.hd"视频流的播放格式 不能够使用Unity自封装的VideoPlayer组件,那是用来播放MP4格式的视频的,不能用于flv,m3u8...
官方购买插件AVPro ------ AVpro v1.9.4 3. 踩过的一个坑 **
965b9c0aaf5ea32bd466d54d390c435.png
朋友分享给我的低版本,电脑端可以,打包到安卓平台 就会被kill,这不就提示我更新插件了米啊!@!安卓打包需要添加一个环境
Androidmainfest文件中增加访问的控制权限
网络访问的权限
<?xml version="1.0" encoding="utf-8"?><!-- GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN--><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" xmlns:tools="http://schemas.android.com/tools"> <application> <activity android:name="com.unity3d.player.UnityPlayerActivity" android:theme="@style/UnityThemeSelector" android:usesCleartextTraffic="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> </activity> </application></manifest>4. http请求
using System;using System.Collections;using System.Collections.Generic;using LitJson;using RenderHeads.Media.AVProVideo;using UnityEngine;using UnityEngine.Events;using UnityEngine.Networking;using UnityEngine.UI;/// <summary>/// 2022 3-12/// http 项目 萤石云视频监控开发 主控制脚本/// </summary>public class Manager : MonoBehaviour{ //属性 private MediaPlayer _mediaPlayer; private MediaPath path; string IP = "https://open.ys7.com/"; //http string http_Url; //请求地址 //appKey和secret可以在官网-开发者服务-我的应用中找到 private string appKey = "752b5e8312094b5bb76b2****"; private string appSecret = "7bcffd435aa12fb73e3bb****"; //根据appKey和secret获取accessToken string getToken_url = "api/lapp/token/get"; string accessToken; // private string getAddress_Url="lapp/v2/live/address/get"; private string deviceSerial = "C78957921"; //设备序列号,存在英文字母的设备序列号,字母需为大写 private int channelNo = 1; // 通道号 默认为1 string url = "http://hls01open.ys7.com/openlive/1adff11698b64e228892****a8ca.m3u8"; //视频播放地址 private Text msg; //startButton private GameObject game; private Button startButton; // 方法 void Start() { _mediaPlayer = FindObjectOfType<MediaPlayer>(); StartCoroutine(GetToken()); msg = _mediaPlayer.GetComponentInChildren<Text>(); // game = GameObject.Find("MediaPlayerUI 1/Video/VideoDisplay/Game").gameObject; // game.gameObject.SetActive(true);//自定义 按钮 // startButton = GameObject.Find("MediaPlayerUI 1/Video/VideoDisplay/Game/StartBtn").GetComponent<Button>(); // startButton.onClick.AddListener(startBtn); } /// <summary> /// 获取accessToken /// 有效期是7天,请在即将过期或者接口报错10002时重新获取,每个token具备独立的7天生命周期,请勿频繁调用避免占用过多接口调用次数。 /// </summary> /// <returns></returns> IEnumerator GetToken(Action<loginManager> callBack = null) { //post 提交方式 采用表单的方式 WWWForm form = new WWWForm(); form.AddField("appKey", appKey); form.AddField("appSecret", appSecret); http_Url = IP + getToken_url; UnityWebRequest request = UnityWebRequest.Post(http_Url, form); yield return request.SendWebRequest(); if (!request.isNetworkError && !request.isHttpError) { if (request.responseCode == 200) { string s = request.downloadHandler.text; // Debug.Log("客户端已经接受http的登录请求:" + s); Http_Login ht = JsonMapper.ToObject<Http_Login>(s); accessToken = ht.data.accessToken; } var response = JsonMapper.ToObject<loginManager>(request.downloadHandler.text); // Debug.Log(response); if (callBack != null) { callBack(response); } } else { Debug.LogError($"发起网络请求失败: 确认登录接口 -{request.error}"); } StartCoroutine(GetLive()); } /// <summary> /// 获取用户下直播视频列表 /// 该接口适用于已经开通过直播的用户,用以获取账号下的视频地址列表。 /// </summary> /// <returns></returns> IEnumerator GetLive(Action<loginManager> callBack = null) { //post 提交方式 采用表单的方式 WWWForm form = new WWWForm(); form.AddField("accessToken", accessToken); // string http_Url1 ="https://open.ys7.com/api/lapp/live/video/list"; string http_Url1 = IP + "api/lapp/live/video/list"; UnityWebRequest request = UnityWebRequest.Post(http_Url1, form); yield return request.SendWebRequest(); if (!request.isNetworkError && !request.isHttpError) { if (request.responseCode == 200) { string s = request.downloadHandler.text; // Debug.Log("客户端已经接受http的获取用户下直播视频列表请求:" + s); Http_Get ht = JsonMapper.ToObject<Http_Get>(s); deviceSerial = ht.data[0].deviceSerial; channelNo = ht.data[0].channelNo; url = ht.data[0].hdAddress; Play(); } var response = JsonMapper.ToObject<loginManager>(request.downloadHandler.text); // Debug.Log(response); if (callBack != null) { callBack(response); } } else { Debug.LogError($"发起网络请求失败: 确认登录接口 -{request.error}"); } //StartCoroutine(getAddress()); } /// <summary> /// 获取播放地址 /// form /// deviceSerial 设备序列号,存在英文字母的设备序列号,字母需为大写 /// </summary> /// <returns></returns> IEnumerator getAddress(Action<loginManager> callBack = null) { //post 提交方式 采用表单的方式 WWWForm form = new WWWForm(); form.AddField("accessToken", accessToken); form.AddField("deviceSerial", deviceSerial); http_Url = "https://open.ys7.com/api/lapp/v2/live/address/get"; // http_Url = IP + address_url; UnityWebRequest request = UnityWebRequest.Post(http_Url, form); yield return request.SendWebRequest(); if (!request.isNetworkError && !request.isHttpError) { if (request.responseCode == 200) { string s = request.downloadHandler.text; Debug.Log("客户端已经接受http的请求:" + s); Http_GetUrl ht = JsonMapper.ToObject<Http_GetUrl>(s); url = ht.data.url; Debug.LogError("url:" + url); Play(); } var response = JsonMapper.ToObject<loginManager>(request.downloadHandler.text); // Debug.Log(response); if (callBack != null) { callBack(response); } } else { Debug.LogError($"发起网络请求失败: 确认登录接口 -{request.error}"); } } /// <summary> /// 播放视频 /// </summary> void Play() { _mediaPlayer.OpenMedia(new MediaPath(url, MediaPathType.AbsolutePathOrURL), autoPlay: true); }//操作命令:0-上,1-下,2-左,3-右,4-左上,5-左下,6-右上,7-右下,8-放大,9-缩小,10-近焦距,11-远焦距 private int direction; private string code; public void startBtn() { Debug.LogError("开始云台控制 "); StartCoroutine(StopPtz()); } /// <summary> /// 云台 /// 开始云台控制 -----------------先调用停止云台控制接口 /// https://open.ys7.com/api/lapp/device/ptz/start /// /// direction 操作命令:0-上,1-下,2-左,3-右,4-左上,5-左下,6-右上,7-右下,8-放大,9-缩小,10-近焦距,11-远焦距 /// speed 云台速度:0-慢,1-适中,2-快,海康设备参数不可为0 /// </summary> /// <returns>code 返回码 msg 返回消息</returns> IEnumerator StartPtz(Action<loginManager> callBack = null) { //post 提交方式 采用表单的方式 WWWForm form = new WWWForm(); form.AddField("accessToken", accessToken); form.AddField("deviceSerial", deviceSerial); form.AddField("channelNo", channelNo); form.AddField("direction", direction); form.AddField("speed", 2); http_Url = "https://open.ys7.com/api/lapp/device/ptz/start"; UnityWebRequest request = UnityWebRequest.Post(http_Url, form); yield return request.SendWebRequest(); if (!request.isNetworkError && !request.isHttpError) { if (request.responseCode == 200) { string s = request.downloadHandler.text; Debug.Log("客户端已经接受http的云台控制请求:" + s); PtzCode ht = JsonMapper.ToObject<PtzCode>(s); code = ht.code; msg.text = ht.msg; Debug.LogError("text:" + msg.text); } var response = JsonMapper.ToObject<loginManager>(request.downloadHandler.text); if (callBack != null) { callBack(response); } } else { Debug.LogError($"发起网络请求失败: 确认登录接口 -{request.error}"); } } /// <summary> /// 停止云台控制 /// 设备停止云台控制 /// https://open.ys7.com/api/lapp/device/ptz/stop /// /// channelNo 通道号 默认为 /// direction 操作命令:0-上,1-下,2-左,3-右,4-左上,5-左下,6-右上,7-右下,8-放大,9-缩小,10-近焦距,11-远焦距 /// </summary> /// <returns></returns> IEnumerator StopPtz(Action<loginManager> callBack = null) { //post 提交方式 采用表单的方式 WWWForm form = new WWWForm(); form.AddField("accessToken", accessToken); form.AddField("deviceSerial", deviceSerial); form.AddField("channelNo", channelNo); form.AddField("direction", direction); http_Url = "https://open.ys7.com/api/lapp/device/ptz/stop"; UnityWebRequest request = UnityWebRequest.Post(http_Url, form); yield return request.SendWebRequest(); if (!request.isNetworkError && !request.isHttpError) { if (request.responseCode == 200) { string s = request.downloadHandler.text; Debug.Log("客户端已经接受设备停止云台控制:" + s); PtzCode ht = JsonMapper.ToObject<PtzCode>(s); code = ht.code; msg.text = ht.msg; Debug.LogError("text:" + msg.text); //先调用停止云台控制接口 StartCoroutine(StartPtz()); } var response = JsonMapper.ToObject<loginManager>(request.downloadHandler.text); if (callBack != null) { callBack(response); } } else { Debug.LogError($"发起网络请求失败: 确认登录接口 -{request.error}"); } } private int command; /// <summary> /// 对设备进行镜像翻转操作(需要设备支持)。 /// </summary> /// <param name="callBack"></param> /// form : /// accessToken 授权过程获取的access_token /// deviceSerial 设备序列号,存在英文字母的设备序列号,字母需为大写 /// channelNo 通道号 /// command 镜像方向:0-上下, 1-左右, 2-中心 /// <returns></returns> IEnumerator mirrorPtz(Action<loginManager> callBack = null) { //post 提交方式 采用表单的方式 WWWForm form = new WWWForm(); form.AddField("accessToken", accessToken); form.AddField("deviceSerial", deviceSerial); form.AddField("channelNo", channelNo); form.AddField("command", command); http_Url = "https://open.ys7.com/api/lapp/device/ptz/mirror"; UnityWebRequest request = UnityWebRequest.Post(http_Url, form); yield return request.SendWebRequest(); if (!request.isNetworkError && !request.isHttpError) { if (request.responseCode == 200) { string s = request.downloadHandler.text; Debug.Log("客户端已经接受对设备进行镜像翻转操作:" + s); } var response = JsonMapper.ToObject<loginManager>(request.downloadHandler.text); if (callBack != null) { callBack(response); } } else { Debug.LogError($"发起网络请求失败: 确认登录接口 -{request.error}"); } }}//定义类public class Data{ public string accessToken; public string deviceSerial; public int channelNo; public string url; public string hdAddress;// HLS高清直播地址 public string code; public string msg;}public class PtzCode{ public Data data; public string code; public string msg;}public class Http_GetUrl{ public Data data; public string id; public string url; public long expireTime;}public class Http_Get { public List<Data> data; public string deviceSerial; public int channelNo; public string deviceName; public string liveAddress;//HLS流畅直播地址 public string hdAddress;// HLS高清直播地址 public string rtmp;// RTMP流畅直播地址 public string rtmpHd;// RTMP高清直播地址 public long beginTime;//开始时间,long格式如1472694964067,精确到毫秒,该字段已无效,将在2017年6月1号去掉 public long endTime;//过期时间,long格式如1472794964067,精确到毫秒,该字段已无效,将在2017年6月1号去掉 public int status; public int exception;}public class loginManager{ public List<Data> data; public string msg; public string code;}public class Http_Login{ public Data data; public string accessToken;//获取的accessToken public long expireTime;//具体过期时间,精确到毫秒} |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|