在網上查了好久,后來終於用一位大佬的源碼搞了出來,經這位大佬的同意,我把這個demo放上來,以方便以后有同樣需求的人能夠輕松的解決掉這個問題,不要像我一樣這么苦呵呵的。
大佬的代碼是winform,我放到了wpf上,代碼都是一樣的
MainWindow.xaml
<Window x:Class="TingXieB.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TingXieB" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="135" Margin="69,29,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="293"/> <Button x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="69,227,0,0" VerticalAlignment="Top" Width="75" Click="button1_Click"/> <Button x:Name="button2" Content="Button" HorizontalAlignment="Left" Margin="300,227,0,0" VerticalAlignment="Top" Width="75" Click="button2_Click"/> </Grid> </Window>
MainWindow.xaml.cs
using NAudio.Wave; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using VoiceRecorder.Audio; namespace TingXieB { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { string session_begin_params; private WaveIn waveIn; private AudioRecorder recorder; private float lastPeak;//說話音量 float secondsRecorded; float totalBufferLength; int Ends = 5; private const int BUFFER_SIZE = 4096; List<VoiceData> VoiceBuffer = new List<VoiceData>(); public MainWindow() { InitializeComponent(); } public void SpeechRecognition()//初始化語音識別 { int ret = (int)ErrorCode.MSP_SUCCESS; string login_params = string.Format("appid=58622e79,word_dir= . ");//appid和msc.dll要配套 #region 參數 /* *sub:本次識別請求的類型 iat 連續語音識別; asr 語法、關鍵詞識別,默認為iat *domain:領域 iat:連續語音識別 asr:語法、關鍵詞識別 search:熱詞 video:視頻 poi:地名 music:音樂 默認為iat。 注意:sub=asr時,domain只能為asr *language:語言 zh_cn:簡體中文 zh_tw:繁體中文 en_us:英文 默認值:zh_cn *accent:語言區域 mandarin:普通話 cantonese:粵語 lmz:四川話 默認值:mandarin *sample_rate:音頻采樣率 可取值:16000,8000 默認值:16000 離線識別不支持8000采樣率音頻 *result_type:結果格式 可取值:plain,json 默認值:plain *result_encoding:識別結果字符串所用編碼格式 GB2312;UTF-8;UNICODE 不同的格式支持不同的編碼: plain:UTF-8,GB2312 json:UTF-8 */ #endregion session_begin_params = "sub=iat,domain=iat,language=zh_cn,accent=mandarin,sample_rate=16000,result_type=plain,result_encoding=utf8"; string Username = ""; string Password = ""; ret = MSCDLL.MSPLogin(Username, Password, login_params); if ((int)ErrorCode.MSP_SUCCESS != ret)//不成功 { //Console.WriteLine("失敗了"); Console.WriteLine("MSPLogin failed,error code:{0}", ret.ToString()); MSCDLL.MSPLogout(); } } private WaveIn CreateWaveInDevice()//WaveIn實例化 { WaveIn newWaveIn = new WaveIn(); //newWaveIn.WaveFormat = new WaveFormat(16000, 16, 1); // 16bit,16KHz,Mono的錄音格式 newWaveIn.WaveFormat = new WaveFormat(16000, 1);//16bit,1KHz 的錄音格式 newWaveIn.DataAvailable += OnDataAvailable; newWaveIn.RecordingStopped += OnRecordingStopped; return newWaveIn; } /// <summary> /// 開始錄音回調函數 保存截獲到的聲音 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void OnDataAvailable(object sender, WaveInEventArgs e) { totalBufferLength += e.Buffer.Length; secondsRecorded = (float)(totalBufferLength / 32000); VoiceData data = new VoiceData(); for (int i = 0; i < 3200; i++) { data.data[i] = e.Buffer[i]; } VoiceBuffer.Add(data); if (lastPeak < 20) Ends = Ends - 1; else Ends = 5; if (Ends == 0) { if (VoiceBuffer.Count() > 5) { RunIAT(VoiceBuffer, session_begin_params);//調用語音識別 } VoiceBuffer.Clear(); Ends = 5; } } /// <summary> /// 錄音結束回調函數 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void OnRecordingStopped(object sender, StoppedEventArgs e) { if (e.Exception != null) { /* MessageBox.Show(String.Format("A problem was encountered during recording {0}", e.Exception.Message));*/ Console.WriteLine("A problem was encountered during recording {0}", e.Exception.Message); } } /// <summary> /// 指針轉字符串 /// </summary> /// <param name="p">指向非托管代碼字符串的指針</param> /// <returns>返回指針指向的字符串</returns> private string PtrToStr(IntPtr p) { List<byte> lb = new List<byte>(); try { while (Marshal.ReadByte(p) != 0) { lb.Add(Marshal.ReadByte(p)); p = p + 1; } } catch (AccessViolationException ex) { SetText(String.Format(ex.Message)); } return Encoding.UTF8.GetString(lb.ToArray()); } //語音識別 private void RunIAT(List<VoiceData> VoiceBuffer, string session_begin_params) { IntPtr session_id = IntPtr.Zero; string rec_result = string.Empty; string hints = "正常結束"; AudioStatus aud_stat = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE; EpStatus ep_stat = EpStatus.ISR_EP_LOOKING_FOR_SPEECH; RecogStatus rec_stat = RecogStatus.ISR_REC_STATUS_SUCCESS; int errcode = (int)ErrorCode.MSP_SUCCESS; session_id = MSCDLL.QISRSessionBegin(null, session_begin_params, ref errcode); if ((int)ErrorCode.MSP_SUCCESS != errcode) { SetText("\nQISRSessionBegin failed! error code:" + errcode); return; } for (int i = 0; i < VoiceBuffer.Count(); i++) { aud_stat = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE; if (i == 0) aud_stat = AudioStatus.ISR_AUDIO_SAMPLE_FIRST; errcode = MSCDLL.QISRAudioWrite(PtrToStr(session_id), VoiceBuffer[i].data, (uint)VoiceBuffer[i].data.Length, aud_stat, ref ep_stat, ref rec_stat); if ((int)ErrorCode.MSP_SUCCESS != errcode) { MSCDLL.QISRSessionEnd(PtrToStr(session_id), null); } } errcode = MSCDLL.QISRAudioWrite(PtrToStr(session_id), null, 0, AudioStatus.ISR_AUDIO_SAMPLE_LAST, ref ep_stat, ref rec_stat); if ((int)ErrorCode.MSP_SUCCESS != errcode) { SetText("\nQISRAudioWrite failed! error code:" + errcode); return; } while (RecogStatus.ISR_REC_STATUS_SPEECH_COMPLETE != rec_stat) { IntPtr rslt = MSCDLL.QISRGetResult(PtrToStr(session_id), ref rec_stat, 0, ref errcode); if ((int)ErrorCode.MSP_SUCCESS != errcode) { SetText("\nQISRGetResult failed, error code: " + errcode); break; } if (IntPtr.Zero != rslt) { string tempRes = PtrToStr(rslt); rec_result = rec_result + tempRes; if (rec_result.Length >= BUFFER_SIZE) { SetText("\nno enough buffer for rec_result !\n"); break; } } } int errorcode = MSCDLL.QISRSessionEnd(PtrToStr(session_id), hints); //語音識別結果 if (rec_result.Length != 0) { SetText(rec_result); //返回錯誤代碼10111時,可調用SpeechRecognition()函數執行MSPLogin } } //寫行 delegate void SetTextCallback(string text); public void SetText(string text) { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. //if (this.textBox1.InvokeRequired) /*if (textBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { this.textBox1.AppendText(text + "\n"); this.textBox1.Refresh(); }*/ this.textBox1.AppendText(text + "\n"); } private void button1_Click(object sender, RoutedEventArgs e) { SpeechRecognition(); totalBufferLength = 0; recorder = new AudioRecorder(); recorder.BeginMonitoring(-1); recorder.SampleAggregator.MaximumCalculated += OnRecorderMaximumCalculated; if (waveIn == null) { waveIn = CreateWaveInDevice(); } waveIn.WaveFormat = new WaveFormat(16000, 1); waveIn.StartRecording(); } void OnRecorderMaximumCalculated(object sender, MaxSampleEventArgs e) { lastPeak = Math.Max(e.MaxSample, Math.Abs(e.MinSample)) * 100; } private void button2_Click(object sender, RoutedEventArgs e) { waveIn.StopRecording();//停止錄音 } } }
MSCDLL.cs
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace TingXieB { public enum SynthStatus { MSP_TTS_FLAG_STILL_HAVE_DATA = 1, MSP_TTS_FLAG_DATA_END = 2, MSP_TTS_FLAG_CMD_CANCELED = 0 } class MSCDLL { public const int GRAMID_LEN = 128; //調用 MSPLogin(...)接口登入,可以只登入一次,但是必須保證在調用其他接口前先登入 [DllImport("msc.dll", CallingConvention = CallingConvention.StdCall)] public static extern int MSPLogin(string userName, string password, string parameter); [DllImport("msc.dll", CallingConvention = CallingConvention.StdCall)] public static extern IntPtr MSPSearch(string paramstr, byte[] text, ref uint dataLen, ref int errorCode); //不再使用服務的時候 調用MSPLogout()登出,避免不必要的麻煩 [DllImport("msc.dll", CallingConvention = CallingConvention.StdCall)] public static extern int MSPLogout(); [DllImport("msc.dll", CallingConvention = CallingConvention.StdCall)] public static extern int QISRInit(string configs); //調用 QISRSessionBegin(...)開始一次語音聽寫 [DllImport("msc.dll", CallingConvention = CallingConvention.StdCall)] public static extern IntPtr QISRSessionBegin(string grammarList, string _params, ref int errorCode); //調用 QISRAudioWrite(...) 分塊寫入音頻數據 [DllImport("msc.dll", CallingConvention = CallingConvention.StdCall)] public static extern int QISRAudioWrite(string sessionID, byte[] waveData, uint waveLen, AudioStatus audioStatus, ref EpStatus epStatus, ref RecogStatus recogStatus); //循環調用 QISRGetResult(...) 接口返回聽寫結果 [DllImport("msc.dll", CallingConvention = CallingConvention.StdCall)] public static extern IntPtr QISRGetResult(string sessionID, ref RecogStatus rsltStatus, int waitTime, ref int errorCode); //調用 QISRSessionEnd(...) 主動結束本次聽寫 [DllImport("msc.dll", CallingConvention = CallingConvention.StdCall)] public static extern int QISRSessionEnd(string sessionID, string hints); [DllImport("msc.dll", CallingConvention = CallingConvention.StdCall)] public static extern int QISRFini(); [DllImport("msc.dll", CallingConvention = CallingConvention.StdCall)] public static extern IntPtr QISRUploadData(string sessionID, string dataName, byte[] userData, uint lenght, string paramValue, ref int errorCode); //語音合成 //[DllImport("msc.dll", CallingConvention = CallingConvention.Winapi)] //public static extern IntPtr QTTSSessionBegin(string _params, ref int errorCode); //[DllImport("msc.dll", CallingConvention = CallingConvention.Winapi)] //public static extern int QTTSTextPut(string sessionID, string textString, uint textLen, string _params); //[DllImport("msc.dll", CallingConvention = CallingConvention.Winapi)] //public static extern IntPtr QTTSAudioGet(string sessionID, ref uint audioLen, ref SynthStatus synthStatus, ref int errorCode); //[DllImport("msc.dll", CallingConvention = CallingConvention.Winapi)] //public static extern IntPtr QTTSAudioInfo(string sessionID); //[DllImport("msc.dll", CallingConvention = CallingConvention.Winapi)] //public static extern int QTTSSessionEnd(string sessionID, string hints); } #region 錯誤代號 public enum ErrorCode { MSP_SUCCESS = 0, //函數執行成功 MSP_ERROR_FAIL = -1, //失敗 MSP_ERROR_EXCEPTION = -2, /* General errors 10100(0x2774) */ MSP_ERROR_GENERAL = 10100, /* 0x2774 */ MSP_ERROR_OUT_OF_MEMORY = 10101, /* 0x2775 */ MSP_ERROR_FILE_NOT_FOUND = 10102, /* 0x2776 */ MSP_ERROR_NOT_SUPPORT = 10103, /* 0x2777 */ MSP_ERROR_NOT_IMPLEMENT = 10104, /* 0x2778 */ MSP_ERROR_ACCESS = 10105, /* 0x2779 */ MSP_ERROR_INVALID_PARA = 10106, /* 0x277A */ MSP_ERROR_INVALID_PARA_VALUE = 10107, /* 0x277B */ MSP_ERROR_INVALID_HANDLE = 10108, /* 0x277C */ MSP_ERROR_INVALID_DATA = 10109, /* 0x277D */ //客戶端上傳的命令詞語法,存在單詞長度超過128字節。 客戶端上傳abnf時,語法內容不是以#ABNF 1.0 UTF-8;和#ABNF 1.0 gb2312;開頭,目前只支持這兩種格式的abnf語法上傳。 攜帶的語法內容為空。 MSP_ERROR_NO_LICENSE = 10110, /* 0x277E */ MSP_ERROR_NOT_INIT = 10111, /* 0x277F */ MSP_ERROR_NULL_HANDLE = 10112, /* 0x2780 */ MSP_ERROR_OVERFLOW = 10113, /* 0x2781 */ MSP_ERROR_TIME_OUT = 10114, /* 0x2782 */ //查看網絡環境是否正常,查看是否使用的是公司網絡(很多公司網絡會將一些網絡地址和端口屏蔽掉),查看訊飛的服務器地址和端口是不是被屏蔽(服務器地址dev.voicecloud.cn 端口號是1028和80)。 MSP_ERROR_OPEN_FILE = 10115, /* 0x2783 */ MSP_ERROR_NOT_FOUND = 10116, /* 0x2784 */ MSP_ERROR_NO_ENOUGH_BUFFER = 10117, /* 0x2785 */ MSP_ERROR_NO_DATA = 10118, /* 0x2786 */ MSP_ERROR_NO_MORE_DATA = 10119, /* 0x2787 */ MSP_ERROR_SKIPPED = 10120, /* 0x2788 */ MSP_ERROR_ALREADY_EXIST = 10121, /* 0x2789 */ MSP_ERROR_LOAD_MODULE = 10122, /* 0x278A */ MSP_ERROR_BUSY = 10123, /* 0x278B */ MSP_ERROR_INVALID_CONFIG = 10124, /* 0x278C */ MSP_ERROR_VERSION_CHECK = 10125, /* 0x278D */ MSP_ERROR_CANCELED = 10126, /* 0x278E */ MSP_ERROR_INVALID_MEDIA_TYPE = 10127, /* 0x278F */ MSP_ERROR_CONFIG_INITIALIZE = 10128, /* 0x2790 */ MSP_ERROR_CREATE_HANDLE = 10129, /* 0x2791 */ MSP_ERROR_CODING_LIB_NOT_LOAD = 10130, /* 0x2792 */ /* Error codes of network 10200(0x27D8)*/ MSP_ERROR_NET_GENERAL = 10200, /* 0x27D8 */ MSP_ERROR_NET_OPENSOCK = 10201, /* 0x27D9 */ /* Open socket */ MSP_ERROR_NET_CONNECTSOCK = 10202, /* 0x27DA */ /* Connect socket */ MSP_ERROR_NET_ACCEPTSOCK = 10203, /* 0x27DB */ /* Accept socket */ MSP_ERROR_NET_SENDSOCK = 10204, /* 0x27DC */ /* Send socket data */ MSP_ERROR_NET_RECVSOCK = 10205, /* 0x27DD */ /* Recv socket data */ MSP_ERROR_NET_INVALIDSOCK = 10206, /* 0x27DE */ /* Invalid socket handle */ MSP_ERROR_NET_BADADDRESS = 10207, /* 0x27EF */ /* Bad network address */ MSP_ERROR_NET_BINDSEQUENCE = 10208, /* 0x27E0 */ /* Bind after listen/connect */ MSP_ERROR_NET_NOTOPENSOCK = 10209, /* 0x27E1 */ /* Socket is not opened */ MSP_ERROR_NET_NOTBIND = 10210, /* 0x27E2 */ /* Socket is not bind to an address */ MSP_ERROR_NET_NOTLISTEN = 10211, /* 0x27E3 */ /* Socket is not listenning */ MSP_ERROR_NET_CONNECTCLOSE = 10212, /* 0x27E4 */ /* The other side of connection is closed */ MSP_ERROR_NET_NOTDGRAMSOCK = 10213, /* 0x27E5 */ /* The socket is not datagram type */ MSP_ERROR_NET_DNS = 10214, //DNS解析錯誤,即域名轉換到IP失敗,可以換個網絡通暢的環境,或者先用固定IP測試。 /* Error codes of mssp message 10300(0x283C) */ MSP_ERROR_MSG_GENERAL = 10300, /* 0x283C */ MSP_ERROR_MSG_PARSE_ERROR = 10301, /* 0x283D */ MSP_ERROR_MSG_BUILD_ERROR = 10302, /* 0x283E */ MSP_ERROR_MSG_PARAM_ERROR = 10303, /* 0x283F */ MSP_ERROR_MSG_CONTENT_EMPTY = 10304, /* 0x2840 */ MSP_ERROR_MSG_INVALID_CONTENT_TYPE = 10305, /* 0x2841 */ MSP_ERROR_MSG_INVALID_CONTENT_LENGTH = 10306, /* 0x2842 */ MSP_ERROR_MSG_INVALID_CONTENT_ENCODE = 10307, /* 0x2843 */ MSP_ERROR_MSG_INVALID_KEY = 10308, /* 0x2844 */ MSP_ERROR_MSG_KEY_EMPTY = 10309, /* 0x2845 */ MSP_ERROR_MSG_SESSION_ID_EMPTY = 10310, /* 0x2846 */ MSP_ERROR_MSG_LOGIN_ID_EMPTY = 10311, /* 0x2847 */ MSP_ERROR_MSG_SYNC_ID_EMPTY = 10312, /* 0x2848 */ MSP_ERROR_MSG_APP_ID_EMPTY = 10313, /* 0x2849 */ MSP_ERROR_MSG_EXTERN_ID_EMPTY = 10314, /* 0x284A */ MSP_ERROR_MSG_INVALID_CMD = 10315, /* 0x284B */ MSP_ERROR_MSG_INVALID_SUBJECT = 10316, /* 0x284C */ MSP_ERROR_MSG_INVALID_VERSION = 10317, /* 0x284D */ MSP_ERROR_MSG_NO_CMD = 10318, /* 0x284E */ MSP_ERROR_MSG_NO_SUBJECT = 10319, /* 0x284F */ MSP_ERROR_MSG_NO_VERSION = 10320, /* 0x2850 */ MSP_ERROR_MSG_MSSP_EMPTY = 10321, /* 0x2851 */ MSP_ERROR_MSG_NEW_RESPONSE = 10322, /* 0x2852 */ MSP_ERROR_MSG_NEW_CONTENT = 10323, /* 0x2853 */ MSP_ERROR_MSG_INVALID_SESSION_ID = 10324, /* 0x2854 */ /* Error codes of DataBase 10400(0x28A0)*/ MSP_ERROR_DB_GENERAL = 10400, /* 0x28A0 */ MSP_ERROR_DB_EXCEPTION = 10401, /* 0x28A1 */ MSP_ERROR_DB_NO_RESULT = 10402, /* 0x28A2 */ MSP_ERROR_DB_INVALID_USER = 10403, /* 0x28A3 */ MSP_ERROR_DB_INVALID_PWD = 10404, /* 0x28A4 */ MSP_ERROR_DB_CONNECT = 10405, /* 0x28A5 */ MSP_ERROR_DB_INVALID_SQL = 10406, /* 0x28A6 */ MSP_ERROR_DB_INVALID_APPID = 10407, /* 0x28A7 */ //請確認申請的Appid是否授權通過; 請確認申請的Appid和下載的SDK是否具有一致性。 /* Error codes of Resource 10500(0x2904)*/ MSP_ERROR_RES_GENERAL = 10500, /* 0x2904 */ MSP_ERROR_RES_LOAD = 10501, /* 0x2905 */ /* Load resource */ MSP_ERROR_RES_FREE = 10502, /* 0x2906 */ /* Free resource */ MSP_ERROR_RES_MISSING = 10503, /* 0x2907 */ /* Resource File Missing */ MSP_ERROR_RES_INVALID_NAME = 10504, /* 0x2908 */ /* Invalid resource file name */ MSP_ERROR_RES_INVALID_ID = 10505, /* 0x2909 */ /* Invalid resource ID */ MSP_ERROR_RES_INVALID_IMG = 10506, /* 0x290A */ /* Invalid resource image pointer */ MSP_ERROR_RES_WRITE = 10507, /* 0x290B */ /* Write read-only resource */ MSP_ERROR_RES_LEAK = 10508, /* 0x290C */ /* Resource leak out */ MSP_ERROR_RES_HEAD = 10509, /* 0x290D */ /* Resource head currupt */ MSP_ERROR_RES_DATA = 10510, /* 0x290E */ /* Resource data currupt */ MSP_ERROR_RES_SKIP = 10511, /* 0x290F */ /* Resource file skipped */ /* Error codes of TTS 10600(0x2968)*/ MSP_ERROR_TTS_GENERAL = 10600, /* 0x2968 */ MSP_ERROR_TTS_TEXTEND = 10601, /* 0x2969 */ /* Meet text end */ MSP_ERROR_TTS_TEXT_EMPTY = 10602, /* 0x296A */ /* no synth text */ /* Error codes of Recognizer 10700(0x29CC) */ MSP_ERROR_REC_GENERAL = 10700, /* 0x29CC */ MSP_ERROR_REC_INACTIVE = 10701, /* 0x29CD */ MSP_ERROR_REC_GRAMMAR_ERROR = 10702, /* 0x29CE */ //10702為沒有有效的語法文件,查看是否已經成功上傳語法文件,上傳的語法文件格式是否是正確的,查看文件中的字符、數字、空格、標點是否是相應的格式(如全角還是半角,英文還是中文,gb2312編碼還是utf-8編碼,實際編碼與文件開頭與代碼參數設置是否一致)。 MSP_ERROR_REC_NO_ACTIVE_GRAMMARS = 10703, /* 0x29CF */ //客戶端沒有攜帶任何語法文件,直接調用語法接口,造成引擎找不到可激活的語法文件。 MSP_ERROR_REC_DUPLICATE_GRAMMAR = 10704, /* 0x29D0 */ MSP_ERROR_REC_INVALID_MEDIA_TYPE = 10705, /* 0x29D1 */ MSP_ERROR_REC_INVALID_LANGUAGE = 10706, /* 0x29D2 */ MSP_ERROR_REC_URI_NOT_FOUND = 10707, /* 0x29D3 */ MSP_ERROR_REC_URI_TIMEOUT = 10708, /* 0x29D4 */ MSP_ERROR_REC_URI_FETCH_ERROR = 10709, /* 0x29D5 */ /* Error codes of Speech Detector 10800(0x2A30) */ MSP_ERROR_EP_GENERAL = 10800, /* 0x2A30 */ MSP_ERROR_EP_NO_SESSION_NAME = 10801, /* 0x2A31 */ MSP_ERROR_EP_INACTIVE = 10802, /* 0x2A32 */ MSP_ERROR_EP_INITIALIZED = 10803, /* 0x2A33 */ /* Error codes of TUV */ MSP_ERROR_TUV_GENERAL = 10900, /* 0x2A94 */ MSP_ERROR_TUV_GETHIDPARAM = 10901, /* 0x2A95 */ /* Get Busin Param huanid*/ MSP_ERROR_TUV_TOKEN = 10902, /* 0x2A96 */ /* Get Token */ MSP_ERROR_TUV_CFGFILE = 10903, /* 0x2A97 */ /* Open cfg file */ MSP_ERROR_TUV_RECV_CONTENT = 10904, /* 0x2A98 */ /* received content is error */ MSP_ERROR_TUV_VERFAIL = 10905, /* 0x2A99 */ /* Verify failure */ /* Error codes of IMTV */ MSP_ERROR_IMTV_SUCCESS = 11000, /* 0x2AF8 */ /* 成功 */ MSP_ERROR_IMTV_NO_LICENSE = 11001, /* 0x2AF9 */ /* 試用次數結束,用戶需要付費 */ MSP_ERROR_IMTV_SESSIONID_INVALID = 11002, /* 0x2AFA */ /* SessionId失效,需要重新登錄通行證 */ MSP_ERROR_IMTV_SESSIONID_ERROR = 11003, /* 0x2AFB */ /* SessionId為空,或者非法 */ MSP_ERROR_IMTV_UNLOGIN = 11004, /* 0x2AFC */ /* 未登錄通行證 */ MSP_ERROR_IMTV_SYSTEM_ERROR = 11005, /* 0x2AFD */ /* 系統錯誤 */ /* Error codes of HCR */ MSP_ERROR_HCR_GENERAL = 11100, MSP_ERROR_HCR_RESOURCE_NOT_EXIST = 11101, /* Error codes of http 12000(0x2EE0) */ MSP_ERROR_HTTP_BASE = 12000, /* 0x2EE0 */ /*Error codes of ISV */ MSP_ERROR_ISV_NO_USER = 13000, /* 32C8 */ /* the user doesn't exist */ } #endregion #region ISR枚舉常量 public enum AudioStatus { ISR_AUDIO_SAMPLE_INIT = 0x00, ISR_AUDIO_SAMPLE_FIRST = 0x01, ISR_AUDIO_SAMPLE_CONTINUE = 0x02, ISR_AUDIO_SAMPLE_LAST = 0x04, ISR_AUDIO_SAMPLE_SUPPRESSED = 0x08, ISR_AUDIO_SAMPLE_LOST = 0x10, ISR_AUDIO_SAMPLE_NEW_CHUNK = 0x20, ISR_AUDIO_SAMPLE_END_CHUNK = 0x40, ISR_AUDIO_SAMPLE_VALIDBITS = 0x7f /* to validate the value of sample->status */ } public enum EpStatus { ISR_EP_NULL = -1, ISR_EP_LOOKING_FOR_SPEECH = 0, ///還沒有檢測到音頻的前端點 ISR_EP_IN_SPEECH = 1, ///已經檢測到了音頻前端點,正在進行正常的音頻處理。 ISR_EP_AFTER_SPEECH = 3, ///檢測到音頻的后端點,后繼的音頻會被MSC忽略。 ISR_EP_TIMEOUT = 4, ///超時 ISR_EP_ERROR = 5, ///出現錯誤 ISR_EP_MAX_SPEECH = 6 ///音頻過大 } public enum RecogStatus { ISR_REC_NULL = -1, ISR_REC_STATUS_SUCCESS = 0, ///識別成功,此時用戶可以調用QISRGetResult來獲取(部分)結果。 ISR_REC_STATUS_NO_MATCH = 1, ///識別結束,沒有識別結果 ISR_REC_STATUS_INCOMPLETE = 2, ///正在識別中 ISR_REC_STATUS_NON_SPEECH_DETECTED = 3, ///保留 ISR_REC_STATUS_SPEECH_DETECTED = 4, ///發現有效音頻 ISR_REC_STATUS_SPEECH_COMPLETE = 5, ///識別結束 ISR_REC_STATUS_MAX_CPU_TIME = 6, ///保留 ISR_REC_STATUS_MAX_SPEECH = 7, ///保留 ISR_REC_STATUS_STOPPED = 8, ///保留 ISR_REC_STATUS_REJECTED = 9, ///保留 ISR_REC_STATUS_NO_SPEECH_FOUND = 10 ///沒有發現音頻 } #endregion }
VoiceData.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TingXieB { class VoiceData { public byte[] data = new byte[3200]; } }
其中用到了三個dll文件
分別是訊飛的msc.dll、還有其余的兩個NAudio.dll和VoiceRecorder.Audio.dll
其中訊飛的msc.dll由於是用dllimport引入的,所以需要放到debug文件夾中,還有就是msc.dll需要用自己配套的訊飛appid和該id應用下載的dll文件才可以正常使用
以下放出連接
NAudio.dll 鏈接:https://pan.baidu.com/s/1hundpEs 密碼:i1m2
VoiceRecorder.Audio.dll 鏈接:https://pan.baidu.com/s/1pMHGS6B 密碼:fvmv