官方参考文档:http://dev.polyv.net/2017/videoproduct/v-playerapi/html5player/html5-docs/
1.上传视频之前根据自己需要对所上传的视频进行加密设置
下面说一下对于加密视频的H5播放 重点就在创建playsafe--获取token
1. 页面播放器加载
<div id="player"></div>
<script src="//player.polyv.net/script/player.js"></script>
<script>
var player = polyvPlayer({
wrap: '#player',
width: 800,
height: 533,
vid: '7d9f485283907d3f2cf12a866910b682_7',
playsafe: function(vid, next) { //不是加密视频playsafe不需要
$.ajax({
type: 'post',
url: '服务端用来获取token的url',
data: {
vid: vid
},
}).done(function (res) {
var result = JSON.parse(res);
playsafe = result.data.token;
next(playsafe);
});
}
});
</script>
2. Token 获取(服务端来获取)
(1) 获取客户端IP
(2) 创建加密生成sign所需参数
(3) 将参数 userid、 videoid、 ts、 viewerip、 viewerid、 viewername、 extraparams 按照ASCII升序 key + value + ... + key + value拼接成字符串
(4) 将拼接好的字符串首尾拼接secretkey
(5) 将拼接好的字符串通过MD5加密 取大写MD5(32位)
(6) post请求polyv 获取token
c#代码如下:
public string GetPolyToken(string vid)
{
var userId = "7d9f485283"; // polyv 提供的服务器间的通讯验证
var secret = "1NM4HdSS9P"; // polyv 提供的接口调用签名访问的key
var ip = "112.53.65.101"; // 客户端真实ip
var timeStamp = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds).ToString(); // 毫秒级时间戳(13位)
var viewerId = "12345"; //观看者id
var viewerName = "12345"; //用户昵称(中文需要urlencode('张三'))
var extraParam = "HTML5"; //自定义参数
// 1. 创建加密生成 sign 的所需参数
Dictionary<string, string> parameters = new Dictionary<string, string>
{
{"userId", userId},
{"videoId", vid},
{"ts", timeStamp},
{"viewerIp", ip},
{"viewerId", viewerId},
{"viewerName", viewerName},
{"extraParams", extraParam}
};
// 2.将参数 userid、 videoid、 ts、 viewerip、 viewerid、 viewername、 extraparams 按照ASCII升序 key + value + ... + key + value 拼接成字符串 ascDicStr
var ascDic = parameters.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value.ToString());
var ascDicStr = "";
foreach (var kv in ascDic)
{
ascDicStr += kv.Key + kv.Value;
}
// 3. 首尾拼接 secretkey
var signStr = secret + ascDicStr + secret;
// 4. 取大写MD5(32位) eg:DFC38BAACA122385A055D99739834A6D
var sign1 = MD5(signStr);
var sign = sign1.Replace("-", "");
// 5. 将生成的 sign 作为参数一并传给polyv用来获取token
parameters.Add("sign",sign);
string serviceUrl = "https://hls.videocc.net/service/v1/token";
//模拟 post 请求
HttpWebRequest request = null;
HttpWebResponse response = null;
Stream reqStream = null;
request = (HttpWebRequest)WebRequest.Create(serviceUrl);
request.Method = "post"; //设置为post请求
request.ReadWriteTimeout = 5000;
request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters, "utf8")); //使用utf-8格式组装post参数
reqStream = request.GetRequestStream();
reqStream.Write(postData, 0, postData.Length);
response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("gb2312"));
string retString = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
return retString;
}
//组装请求参数
private string BuildQuery(IDictionary<string, string> parameters, string encode)
{
StringBuilder postData = new StringBuilder();
bool hasParam = false;
IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
while (dem.MoveNext())
{
string name = dem.Current.Key;
string value = dem.Current.Value;
// 忽略参数名或参数值为空的参数
if (!string.IsNullOrEmpty(name))
{
if (hasParam)
{
postData.Append("&");
}
postData.Append(name);
postData.Append("=");
if (encode == "gb2312")
{
postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312")));
}
else if (encode == "utf8")
{
postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
}
else
{
postData.Append(value);
}
hasParam = true;
}
}
return postData.ToString();
}
/// <summary>
/// MD5 hash加密
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string MD5(string s)
{
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
var result = BitConverter.ToString(md5.ComputeHash(UnicodeEncoding.UTF8.GetBytes(s.Trim())));
return result;
}