机器人的语音识别和音频记录的同时机器人、音频、语音识别

2023-09-05 07:53:35 作者:老梗剧情

我的应用程序记录在AsyncTask的使用MediaRecorder类,也可以使用谷歌API转换语音到文本音频 - 识别意图 - 使用code,从这样一个问题:How我可以使用没有在Android手机烦人的对话框

语音识别

我试图也录制音频在螺纹,但是这是更糟溶液。它会导致更多的问题。我的问题是我的应用程序正常运行的模拟器。但模拟器因为缺乏语音识别服务不支持语音reocognition。而我的设备上我的应用程序,当我开始录制的音频和语音reognizing崩溃 - 意外停止。然而,当我具备WiFi关闭,应用程序正常工作就像模拟器。

录制音频,需要在AndroidManifest:

 <使用-权限的Andr​​oid:名称=android.permission.RECORD_AUDIO/>
 

和语音识别requiers:

 <使用-权限的Andr​​oid:名称=android.permission.RECORD_AUDIO/>
<使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
 

我想这是问题,单一的音频输入?我怎样才能解决这个问题呢? 谷歌语音识别requiers在主UI线程工作,所以我不能例如做它在异步任务。所以,我有录音的异步任务。我不知道为什么这会导致问题。

我已经连接我的设备到Eclipse和我已经使用USB调试。这是execption我在LogCat中:

  08-23 14:50:03.528:ERROR / ActivityThread(12403):活动go.android.Activity渗漏ServiceConnection android.speech.SpeechRecognizer$Connection@48181340这本来是束缚这里
08-23 14:50:03.528:ERROR / ActivityThread(12403):android.app.ServiceConnectionLeaked:活动go.android.Activity渗漏ServiceConnection android.speech.SpeechRecognizer$Connection@48181340最初这里必然
。08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.app.ActivityThread $ PackageInfo $ ServiceDispatcher< INIT>(ActivityThread.java:1121)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.app.ActivityThread $ PackageInfo.getServiceDispatcher(ActivityThread.java:1016)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.app.ContextImpl.bindService(ContextImpl.java:951)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.content.ContextWrapper.bindService(ContextWrapper.java:347)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.speech.SpeechRecognizer.startListening(SpeechRecognizer.java:267)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在go.android.Activity.startRecordingAndAnimation(Activity.java:285)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在go.android.Activity.onResume(Activity.java:86)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1151)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.app.Activity.performResume(Activity.java:3823)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.os.Handler.dispatchMessage(Handler.java:99)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.os.Looper.loop(Looper.java:123)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在android.app.ActivityThread.main(ActivityThread.java:4627)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在java.lang.reflect.Method.invokeNative(本机方法)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在java.lang.reflect.Method.invoke(Method.java:521)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-23 14:50:03.528:ERROR / ActivityThread(12403):在dalvik.system.NativeStart.main(本机方法)
 
镇江短信发送哪家稳定,语音推广平台报价

和另一个异常后:

  08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):无法创建会话
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):com.google.android.voicesearch.speechservice.ConnectionException:POST失败
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在com.google.android.voicesearch.speechservice.SpeechServiceHttpClient.post(SpeechServiceHttpClient.java:176)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在com.google.android.voicesearch.speechservice.SpeechServiceHttpClient.post(SpeechServiceHttpClient.java:88)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在com.google.android.voicesearch.speechservice.ServerConnectorImpl.createTcpSession(ServerConnectorImpl.java:118)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在com.google.android.voicesearch.speechservice.ServerConnectorImpl.createSession(ServerConnectorImpl.java:98)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在com.google.android.voicesearch.speechservice.RecognitionController.runRecognitionMainLoop(RecognitionController.java:679)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在com.google.android.voicesearch.speechservice.RecognitionController.startRecognition(RecognitionController.java:463)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在com.google.android.voicesearch.speechservice.RecognitionController.access$200(RecognitionController.java:75)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在com.google.android.voicesearch.speechservice.RecognitionController$1.handleMessage(RecognitionController.java:300)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在android.os.Handler.dispatchMessage(Handler.java:99)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在android.os.Looper.loop(Looper.java:123)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在android.os.HandlerThread.run(HandlerThread.java:60)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):由:java.net.SocketTimeoutException
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:564)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:88)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.http.protocol.Htt prequestExecutor.doReceiveResponse(Htt的prequestExecutor.java:279)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.http.protocol.Htt prequestExecutor.execute(Htt的prequestExecutor.java:121)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:410)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在android.net.http.AndroidHttpClient.execute(AndroidHttpClient.java:243)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):在com.google.android.voicesearch.speechservice.SpeechServiceHttpClient.post(SpeechServiceHttpClient.java:167)
08-23 14:50:08.000:ERROR / ServerConnectorImpl(12412):10 ...更多
08-23 14:50:08.000:ERROR / RecognitionController(12412):忽略错误2
 

解决方案

我得到的运作良好具有语音识别和音频录制解决方案。这里是链接以我创建的显示解决方案的工作简单的Andr​​oid项目。另外,我把一些打印屏幕上的项目中说明的应用程序。

我会尽量简明扼要地解释我所使用的方法。我结合该项目中两个特点:谷歌语音API和FLAC记录

谷歌语音API是通过HTTP连接调用。 迈克Pultz 提供有关API的更多细节:

(...)新(谷歌)API是一个全双工流API。这句话的意思是,它实际上使用两个HTTP连接 - 一个职位的要求上载的内容为活分块流,和一个第二GET请求来访问的结果,这使得更多的意义为更长的音频样本,或流式音频。的

然而,该API需要接收FLAC声音文件才能正常工作。这使得我们去的第二部分:拉克记录

我通过提取和从一个开源应用程序称为AudioBoo适应几张code和库中实现拉克记录在该项目。 AudioBoo使用本机code录制和播放FLAC格式。

因此​​,它可以记录后手的声音,将其发送给谷歌语音API,获取文本,并播放刚刚录制的声音。

我创造了该项目的基本原则,使其工作,可以针对具体情况加以改进。为了使其在不同的情况下工作,它需要得到谷歌语音API密钥,它是由被谷歌铬开发组的一部分获得。我离开一键在该项目只是为了显示它的工作,但我会最终将其删除。如果有人需要更多的相关信息,让我知道,因为我不能把2个以上的链接在这个岗位。

My application records audio using MediaRecorder class in AsyncTask and also use Google API transform speech to text - Recognizer Intent - using the code from this question : How can I use speech recognition without the annoying dialog in android phones

I have tried also to record audio in Thread, but this is worse solution. It causes more problems. My problem is that my application works properly on emulator. But emulator don't supports speech reocognition because of lack of voice recognition services. And on my device my application has crash when I starts recording audio and speech reognizing - "has stopped unexpectedly". However when I have wifi turned off, application works properly like on emulator.

Recording audio requires in AndroidManifest:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

and speech recognition requiers:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />

I suppose this is problem with single audio input? How can I resolve this problem? Google Speech Recognizer requiers to work in main UI thread, so I can't for example do it in Async Task. So I have audio recording in Async Task. I don't have idea why this causes problems.

I have connected my device to Eclipse and I have used USB debugging. And this is execption I have in LogCat:

08-23 14:50:03.528: ERROR/ActivityThread(12403): Activity go.android.Activity has leaked ServiceConnection android.speech.SpeechRecognizer$Connection@48181340 that was originally bound here
08-23 14:50:03.528: ERROR/ActivityThread(12403): android.app.ServiceConnectionLeaked: Activity go.android.Activity has leaked ServiceConnection android.speech.SpeechRecognizer$Connection@48181340 that was originally bound here
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.app.ActivityThread$PackageInfo$ServiceDispatcher.<init>(ActivityThread.java:1121)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.app.ActivityThread$PackageInfo.getServiceDispatcher(ActivityThread.java:1016)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.app.ContextImpl.bindService(ContextImpl.java:951)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.speech.SpeechRecognizer.startListening(SpeechRecognizer.java:267)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at go.android.Activity.startRecordingAndAnimation(Activity.java:285)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at go.android.Activity.onResume(Activity.java:86)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1151)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.app.Activity.performResume(Activity.java:3823)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.os.Looper.loop(Looper.java:123)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at java.lang.reflect.Method.invoke(Method.java:521)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-23 14:50:03.528: ERROR/ActivityThread(12403):     at dalvik.system.NativeStart.main(Native Method)

And after that another exception:

08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412): Failed to create session
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412): com.google.android.voicesearch.speechservice.ConnectionException: POST failed
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at com.google.android.voicesearch.speechservice.SpeechServiceHttpClient.post(SpeechServiceHttpClient.java:176)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at com.google.android.voicesearch.speechservice.SpeechServiceHttpClient.post(SpeechServiceHttpClient.java:88)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at com.google.android.voicesearch.speechservice.ServerConnectorImpl.createTcpSession(ServerConnectorImpl.java:118)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at com.google.android.voicesearch.speechservice.ServerConnectorImpl.createSession(ServerConnectorImpl.java:98)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at com.google.android.voicesearch.speechservice.RecognitionController.runRecognitionMainLoop(RecognitionController.java:679)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at com.google.android.voicesearch.speechservice.RecognitionController.startRecognition(RecognitionController.java:463)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at com.google.android.voicesearch.speechservice.RecognitionController.access$200(RecognitionController.java:75)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at com.google.android.voicesearch.speechservice.RecognitionController$1.handleMessage(RecognitionController.java:300)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at android.os.Looper.loop(Looper.java:123)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at android.os.HandlerThread.run(HandlerThread.java:60)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412): Caused by: java.net.SocketTimeoutException
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:564)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:88)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:410)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at android.net.http.AndroidHttpClient.execute(AndroidHttpClient.java:243)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     at com.google.android.voicesearch.speechservice.SpeechServiceHttpClient.post(SpeechServiceHttpClient.java:167)
08-23 14:50:08.000: ERROR/ServerConnectorImpl(12412):     ... 10 more
08-23 14:50:08.000: ERROR/RecognitionController(12412): Ignoring error 2

解决方案

I got a solution that is working well to have speech recognizing and audio recording. Here is the link to a simple Android project I created to show the solution's working. Also, I put some print screens inside the project to illustrate the app.

I'm gonna try to explain briefly the approach I used. I combined two features in that project: Google Speech API and Flac recording.

Google Speech API is called through HTTP connections. Mike Pultz gives more details about the API:

"(...) the new [Google] API is a full-duplex streaming API. What this means, is that it actually uses two HTTP connections- one POST request to upload the content as a "live" chunked stream, and a second GET request to access the results, which makes much more sense for longer audio samples, or for streaming audio."

However, this API needs to receive a FLAC sound file to work properly. That makes us to go to the second part: Flac recording

I implemented Flac recording in that project through extracting and adapting some pieces of code and libraries from an open source app called AudioBoo. AudioBoo uses native code to record and play flac format.

Thus, it's possible to record a flac sound, send it to Google Speech API, get the text, and play the sound that was just recorded.

The project I created has the basic principles to make it work and can be improved for specific situations. In order to make it work in a different scenario, it's necessary to get a Google Speech API key, which is obtained by being part of Google Chromium-dev group. I left one key in that project just to show it's working, but I'll remove it eventually. If someone needs more information about it, let me know cause I'm not able to put more than 2 links in this post.