private void Open_consumer(string _MQRecTopic) { try { if (m_consumer != null) { m_consumer.Close(); m_consumer = null; GC.Collect(); } m_consumer = SingletonInfo.GetInstance().m_mq.CreateConsumer(false, _MQRecTopic);//表示是queue模式 20190215 m_consumer.Listener += new MessageListener(consumer_listener); } catch (System.Exception ex) { LogHelper.WriteLog(typeof(MainForm), "MQ生产者、消费者初始化失败!", "2");//日志测试 20180319 } }
void MainForm_Load(object sender, EventArgs e) { CheckIniConfig(); if (SingletonInfo.GetInstance().FTPEnable) { InitFTPServer(); } Thread.Sleep(SingletonInfo.GetInstance().StartDelay); LogHelper.WriteLog(typeof(MainForm), "语音服务启动!", "4"); LogMessage("语音服务启动!"); DealMqConnection(); //if (isConn) //{ tm = new System.Timers.Timer(); tm.Interval = SingletonInfo.GetInstance().CheckMQInterval; tm.Enabled = true; tm.Elapsed += tm_Elapsed; // } this.Text = "语音服务V_" + Application.ProductVersion; }
void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { if (GetActiveMQConnection(SingletonInfo.GetInstance().CheckMQURL)) { //连接正常 if (!isConn) { DealMqConnection(); } } else { LogMessage("MQ服务器未连接!"); LogHelper.WriteLog(typeof(MainForm), "打开MQ网站出错", "2");//日志测试 20180319 isConn = false; //连接异常 m_consumer.Close(); if (SingletonInfo.GetInstance().m_mq != null) { SingletonInfo.GetInstance().m_mq.Close(); } SingletonInfo.GetInstance().m_mq = null; GC.Collect(); } } catch (Exception ex) { isConn = false; //连接异常 m_consumer.Close(); if (SingletonInfo.GetInstance().m_mq != null) { SingletonInfo.GetInstance().m_mq.Close(); } SingletonInfo.GetInstance().m_mq = null; GC.Collect(); } }
private bool OpenMQ(string _MQURL, string _MQUser, string _MQPWD) { string txtURI = ""; txtURI = _MQURL; try { SingletonInfo.GetInstance().m_mq = null;//多次连接后会建立多个消费者 GC.Collect(); SingletonInfo.GetInstance().m_mq = new MQ(); SingletonInfo.GetInstance().m_mq.uri = txtURI; SingletonInfo.GetInstance().m_mq.username = _MQUser; SingletonInfo.GetInstance().m_mq.password = _MQPWD; SingletonInfo.GetInstance().m_mq.Start(); isConn = true; } catch (System.Exception ex) { isConn = false; LogHelper.WriteLog(typeof(MainForm), "连接MQ服务器异常,请检查端口号、IP地址、用户名及密码是否正确!", "2");//日志测试 20180319 } return(isConn); }
private void SaveFile(string FileName, string Content) { try { DelFile(FileName); if (SingletonInfo.GetInstance().IsNationFlag) { string[] FileNamesp = FileName.Split('.'); FileName = FileNamesp[0] + ".wav"; } SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpVoice Voice = new SpVoice(); Voice.Rate = SingletonInfo.GetInstance()._rate; string filename = FileName; string filenametmp = FileName.Split('.')[0] + "tmp.wav"; string texttxt = Content; SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite; SpFileStream SpFileStream = new SpFileStream(); SpFileStream.Open(filenametmp, SpFileMode, false); Voice.AudioOutputStream = SpFileStream; Voice.Speak(texttxt, SpFlags); Voice.WaitUntilDone(Timeout.Infinite); SpFileStream.Close(); int nFrontPackCnt = SingletonInfo.GetInstance()._nFrontPackCnt; int nTailPackCnt = SingletonInfo.GetInstance()._nTailPackCnt; int de = NativeMethods.InsertBlankAudio(filename, filenametmp, nFrontPackCnt, nTailPackCnt); DelFile(filenametmp); FileInfo MyFileInfo = new FileInfo(filename); float dirTime = (float)MyFileInfo.Length / 32000; string[] filepathname = FileName.Split('\\'); string filenamesignal = ""; if (SingletonInfo.GetInstance().IsNationFlag) { filenamesignal = filepathname[filepathname.Length - 1].Split('.')[0] + ".mp3"; } else { filenamesignal = filepathname[filepathname.Length - 1]; } string senddata = "PACKETTYPE~TTS|FILE~" + filenamesignal + "|TIME~" + ((uint)dirTime).ToString(); LogMessage(senddata); #region 如果是需要mp3文件 则需要调用ffmepg if (SingletonInfo.GetInstance().IsNationFlag) { //转换MP3 string wavfilename = filenamesignal.Replace(".mp3", ".wav"); string fromMusic = SingletonInfo.GetInstance()._path + "\\" + wavfilename; //转换音乐路径 string toMusic = SingletonInfo.GetInstance()._path + "\\" + filenamesignal; //转换后音乐路径 int bitrate = 128 * 1000; //恒定码率 string Hz = "44100"; //采样频率 ExcuteProcess("ffmpeg.exe", "-y -ab " + bitrate + " -ar " + Hz + " -i \"" + fromMusic + "\" \"" + toMusic + "\""); // Thread.Sleep(100); File.Delete(fromMusic); //转换完成 } #endregion #region ftp文件传输 20190107新增 if (SingletonInfo.GetInstance().FTPEnable) { string ftppath = filenamesignal; string path = SingletonInfo.GetInstance()._path + "\\" + filenamesignal; SingletonInfo.GetInstance().ftphelper.UploadFile(path, ftppath);//阻塞式非线程模式 } #endregion SendMQMessage(senddata); } catch (Exception ex) { LogHelper.WriteLog(typeof(MainForm), "SaveFile处理异常:" + ex.ToString() + "----------" + ex.InnerException + "-------" + ex.StackTrace + "-------" + ex.Message, "2"); } }
/// <summary> /// 处理MQ登录信息 /// </summary> private void DealMqConnection() { if (OpenMQ(SingletonInfo.GetInstance().MQURL, SingletonInfo.GetInstance().MQUSER, SingletonInfo.GetInstance().MQWD)) { Open_consumer(SingletonInfo.GetInstance().TopicName1); //创建消息消费者 SingletonInfo.GetInstance().m_mq.CreateProducer(false, SingletonInfo.GetInstance().TopicName2); //创建消息生产者 //Queue LogMessage("MQ连接成功!"); isConn = true; } else { LogMessage("MQ连接失败!"); isConn = false; } }