三、Audio To Text

本节来概要的谈一下利用Google的Api实现将语音转换为文字

请求的URL为:http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN  
         http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1
参数:
1、xjerr  ???参数功能不详,值只能是0或1,去掉也能正常获得结果;
2、client  客户端类型
3、lang    语言类型,英文为en-US。更多参见http://msdn.microsoft.com/en-us/library/ms533052(v=vs.85).aspx
4、maxresults  最大返回结果数量,多个结果在hypotheses(请求返回的数据项)列表中保存。

使用方式:

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(GoogleSTT());
        }
        private string GoogleSTT()
        {
            string result = string.Empty;

            string inFile = @"F:\hebeidaxue.wav";
            FileStream fs = new FileStream(inFile, FileMode.Open);
            byte[] voice = new byte[fs.Length];
            fs.Read(voice, 0, voice.Length);
            fs.Close();

            HttpWebRequest request = null;
            string url = "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN";
            Uri uri = new Uri(url);
            request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";
            request.ContentType = "audio/L16; rate=16000";
            request.ContentLength = voice.Length;
            using (Stream writeStream = request.GetRequestStream())
            {
                writeStream.Write(voice, 0, voice.Length);
            }
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        result = readStream.ReadToEnd();
                    }
                }
            }

            return result;
        }

返回结果的格式

{"status":0,"id":"a44e9fab4888a7713175a07b035d49ad-1","hypotheses":[{"utterance":"大家早上好","confidence":0.7236291}]}

 

其实就是通过Http向google发送语音请求,返回的是识别的内容。
需要注意: request.ContentType
google默认的是request.ContentType = "audio/x-flac; rate=16000; 即:支持flac格式的语音。

其他的语音格式:

 1、WAV格式

    请求Header:Content-Type: audio/L16; rate=16000

    返回结果:识别成功

    2、MP3格式

    请求Header:Content-Type: audio/mpeg; rate=16000

    返回结果:无法识别的编码

 

    请求Header:Content-Type: audio/mpeg3; rate=16000

    返回结果:无法识别的编码

    请求Header:Content-Type: audio/x-mpeg; rate=16000

    返回结果:无法识别的编码

 

    请求Header:Content-Type: audio/x-mpeg-3; rate=16000

    返回结果:无法识别的编码

    请求Header:Content-Type: audio/mp3; rate=16000

    返回结果:无法识别的编码

    3、PCM格式

    请求Header:Content-Type: audio/x-ogg-pcm; rate=16000

    返回结果:无法识别的编码

    请求Header:Content-Type: audio/pcm; rate=16000

    返回结果:无法识别的编码

    4、SPEEX格式

    请求Header:Content-Type: audio/x-speex-with-header-byte; rate=16000

    返回结果:识别成功

    请求Header:Content-Type: audio/speex; rate=16000

    返回结果:识别成功

无法识别的类型,就只能转码之后在去请求了。

 

本机测试语音时候可以通过软件Audacity去录音(rate=16000),然后保存为想要的格式。

Android Studio中的语音识别功能通常通过Google的Speech-to-Text API来实现,这个API允许应用将用户的语音输入转换为文本。要在Android Studio项目中集成语音识别: 1. 首先,你需要在Google Cloud Platform上创建一个项目,并启用Speech-to-Text服务,获取API密钥。 2. 在Android Studio中,打开你的项目设置(File > Project Structure),然后选择"Libraries" -> "Add Library",搜索并添加`play-services-speech`库,这是Google Play Services Speech API的依赖。 3. 在Manifest.xml文件中添加权限,例如 `<uses-permission android:name="android.permission.RECORD_AUDIO" />` 和 `<uses-feature android:name="android.hardware.microphone" />`,以确保应用可以访问麦克风。 4. 使用`SpeechRecognizer`类创建一个`RecognitionListener`,当语音识别完成时,会回调给这个监听器处理结果。在代码中初始化SpeechRecognizer,如下面的例子所示: ```java import com.google.android.speech.recognition.RecognizerIntent; ... private void startVoiceRecognition() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName()); startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT); } ``` 5. 当用户停止录音并返回结果时,你会在`onActivityResult()`方法中解析和处理识别出的文本。 6. 最后别忘了处理错误和异常,因为语音识别可能会因网络连接、设备限制等问题失败。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值