Example #1
0
        private string getReqSign(List <KeyValuePair <string, string> > m_params, string appkey /* 字符串*/)
        {
            // 1. 字典升序排序
            m_params.Sort(Compire);

            // 2. 拼按URL键值对
            var str = "";

            foreach (var keyvalue in m_params)
            {
                if (keyvalue.Value != "")
                {
                    str += keyvalue.Key + '=' + keyvalue.Value + '&';
                }
            }
            // 3. 拼接app_key
            str += "app_key=" + appkey;

            // 4. MD5运算+转换大写,得到请求签名
            var sign = (AudioHelper.md5(str)).ToUpper();

            return(sign);
        }
Example #2
0
        /// <summary>
        /// 获取音频
        /// </summary>
        /// <param name="text"></param>
        /// <param name="OnGet"></param>
        /// <returns></returns>
        public IEnumerator GetAudioClip(string text, UnityAction <AudioClip> OnGet, Params paramss = null)
        {
            text = text.Replace(" ", "");//腾讯好像不支持空格
            if (paramss == null)
            {
                paramss = defultParams;
            }
            waitSpeekQueue.Enqueue(new KeyValuePair <string, Params>(text, paramss));
            if (waitSpeekQueue.Count > 1)
            {
                yield break;
            }

            while (waitSpeekQueue.Count > 0)//有其他协程在工作
            {
                var item = waitSpeekQueue.Dequeue();
                item.Value.text = item.Key;
                System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
                long            timeStamp = (long)(DateTime.Now - startTime).TotalSeconds;                         // 相差毫秒数
                item.Value.time_stamp = (int)timeStamp;
                item.Value.text       = text;
                item.Value.format     = (int)Format.WAV;//暂时只支持这种格式
                item.Value.sign       = "";
                item.Value.sign       = getReqSign(item.Value.GetParams(), appkey);
                var url       = string.Format("{0}?{1}", urlTemp, item.Value.ToParamsString());
                var audioName = AudioHelper.md5(text + item.Value.speaker + item.Value.speed) + ".wav";
                //WebGL直接从指定的目录加载对应的文件(缓存)
                if (Application.platform == RuntimePlatform.WebGLPlayer)
                {
                    bool local = false;
                    yield return(AudioHelper.LoadFromFile(AudioPath, audioName, new UnityAction <AudioClip>(x =>
                    {
                        if (x != null)
                        {
                            local = true;
                            OnGet.Invoke(x);
                        }
                    })));

                    if (!local)
                    {
                        yield return(IntenalDownLand(url, audioName, data =>
                        {
                            var audio = AudioHelper.CreateWavAudio(audioName, data);
                            OnGet(audio);
                        }));
                    }
                }
                else
                {
                    var path = AudioPath + "/" + audioName;
                    if (!File.Exists(path))
                    {
                        //下载并缓存
                        yield return(IntenalDownLand(url, audioName, new UnityAction <byte[]>(data =>
                        {
                            var audio = AudioHelper.CreateWavAudio(audioName, data);
                            OnGet(audio);
                            File.WriteAllBytes(AudioPath + "/" + audioName, data);
                        })));
                    }
                    else
                    {
                        yield return(AudioHelper.LoadFromFile(AudioPath, audioName, OnGet));
                    }
                }
            }
        }