Example #1
0
        private void Play(string txt)
        {
            mciSendString("close app", "", 0, 0);
            string PlayUrlz = string.Format(rest, txt, lan, per, ctp, cuid, token, spd, pit, vol);

            HttpWebRequest req = ToolClass.getRequest("http://tsn.baidu.com/text2audio");

            req.Method        = "POST";
            req.ContentType   = "application/x-www-form-urlencoded";
            req.ContentLength = Encoding.UTF8.GetByteCount(PlayUrlz);
            using (StreamWriter sw = new StreamWriter(req.GetRequestStream())) {
                sw.Write(PlayUrlz);
                //sw.Close();
            }


            HttpWebResponse res = req.GetResponse() as HttpWebResponse;

            using (Stream stream = res.GetResponseStream())
            {
                string strFullFileName = Application.StartupPath + "/app.mp3";
                using (FileStream fs = new FileStream(strFullFileName, FileMode.Truncate | FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    stream.CopyTo(fs);
                    //stream.Close();
                }

                if (mciSendString(string.Format("open \"{0}\" alias app", strFullFileName), null, NULL, NULL) == ERROR_SUCCESS)
                {
                    mciSendString("play app", null, NULL, NULL);
                }
            }
        }
Example #2
0
        private void Btn_Up(object sender, MouseEventArgs e)
        {
            try
            {
                recorder.RecStop();
            }catch (NullReferenceException)
            {}

            recorder = null;
            //Boolean flag=false;
            string voiceStr = Post(Application.StartupPath + "\\test.wav");

            if (!voiceStr.Contains("3301"))
            {
                if (voiceStr.Contains("关闭"))
                {
                    Play("欢迎下次使用!");

                    //主程停止1秒
                    Thread.Sleep(1500);
                    //该方法会强制进程关闭,并给操作系统一个退出代码。
                    Environment.Exit(0);
                    //调用 Application.Exit() 并不一定能让程序立即退出,程序会等待所有的前台线程终止后才能真正退出。
                    //Application.Exit();
                }
                string flag = ToolClass.openBrowser(voiceStr);
                if (flag == "")
                {
                    textBox1.Visible = true;
                    //textBox1.Text = voiceStr + ":未包含定义的字符!";
                    textBox1.Text = "指令未包含定义的字符!";
                }
                else
                {
                    textBox1.Text = "已打开" + flag;
                }
            }
            else
            {
                textBox1.Visible = true;
                textBox1.Text    = voiceStr;
            }


            button1.Text      = "按住说话";
            button1.BackColor = Color.FromArgb(255, 255, 255);
            string text = textBox1.Text;

            Play(text);
        }
Example #3
0
        //获取token值
        public static string getStrToken(string para_API_key, string para_API_secret_key)
        {
            //方法参数说明:
            //para_API_key:API_key(你的KEY)
            //para_API_secret_key(你的SECRRET_KEY)

            //方法返回值说明:
            //百度认证口令码,access_token
            string access_html  = null;
            string access_token = null;
            string getAccessUrl = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" +
                                  "&client_id=" + para_API_key + "&client_secret=" + para_API_secret_key;



            try
            {
                HttpWebRequest getAccessRequest = ToolClass.getRequest(getAccessUrl);
                getAccessRequest.Timeout = 30000;//30秒连接不成功就中断
                getAccessRequest.Method  = "post";

                HttpWebResponse response = getAccessRequest.GetResponse() as HttpWebResponse;
                using (StreamReader strHttpComback = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    access_html = strHttpComback.ReadToEnd();
                }
            }
            catch (WebException ex)
            {
                MessageBox.Show(ex.ToString());
            }

            JObject jo = JObject.Parse(access_html);

            access_token = jo["access_token"].ToString();//得到返回的toke
            return(access_token);
        }
Example #4
0
        private string Post(string audioFilePath)
        {
            //string token = "24.4025c0457c09f7176ac73466f1c22cbe.2592000.1554354708.282335-15681764";dev_pid=1536
            //serverURL += "?lan=zh&cuid=QMS_Sway&token=" + token;
            serverURL += "?dev_pid=1536&cuid=QMS_Sway&token=" + token;
            FileStream fs = new FileStream(audioFilePath, FileMode.Open);

            byte[] voice = new byte[fs.Length];
            fs.Read(voice, 0, voice.Length);
            fs.Close();
            fs.Dispose();

            HttpWebRequest request = ToolClass.getRequest(serverURL);

            //var data = File.ReadAllBytes(audioFilePath);
            request.Method  = "POST";
            request.Timeout = 10000;

            request.ContentType = "audio/wav; rate=16000";
            try
            {
                using (Stream writeStream = request.GetRequestStream())
                {
                    writeStream.Write(voice, 0, voice.Length);
                    writeStream.Close();
                    writeStream.Dispose();
                }
            }
            catch
            {
                return("出错了");
            }
            string result       = string.Empty;
            string result_final = string.Empty;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        string        line = string.Empty;
                        StringBuilder sb   = new StringBuilder();
                        while (!readStream.EndOfStream)
                        {
                            line = readStream.ReadLine();
                            sb.Append(line);
                            sb.Append("\r");
                        }
                        readStream.Close();
                        readStream.Dispose();
                        result = sb.ToString();
                        string[] indexs = result.Split(',');
                        foreach (string index in indexs)
                        {
                            string[] _indexs = index.Split('"');

                            if (_indexs[2] == ":[")
                            {
                                result_final = _indexs[3];
                            }
                            //{"err_msg":"speech quality error.","err_no":3301,"sn":"33021332371551857418"}

                            if (_indexs[2] == ":3301")
                            {
                                return("3301:声音不清晰!");
                            }
                        }
                    }
                    responseStream.Close();
                    responseStream.Dispose();
                }
                response.Close();
            }
            return(result_final);
        }