我正在尝试在语音识别时进行自定义对话,而不是使用官方对话。我得到了那个部分,但是当我决定在识别时显示声音的幅度,以使其更花哨,就像 Google Now 搜索栏所做的那样(它围绕麦克风的圆圈会随着声音变大而变大):
然后我开始编写如何获取声音幅度的代码,最后我用 AudioRecord 类得到了它。
当我尝试混合两者(SpeechRecognizer 和 AudioRecord)时,问题就出现了,因为他们似乎无法共享麦克风,或者类似的东西......
在 logcat 我有这个错误:
03-03 21:16:07.461: E/ListenerAdapter(23359): onError
03-03 21:16:07.461: E/ListenerAdapter(23359): com.google.android.speech.embedded.Greco3RecognitionEngine$EmbeddedRecognizerUnavailableException: Embedded recognizer unavailable
03-03 21:16:07.461: E/ListenerAdapter(23359): at com.google.android.speech.embedded.Greco3RecognitionEngine.startRecognition(Greco3RecognitionEngine.java:108)
03-03 21:16:07.461: E/ListenerAdapter(23359): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 21:16:07.461: E/ListenerAdapter(23359): at java.lang.reflect.Method.invoke(Method.java:511)
03-03 21:16:07.461: E/ListenerAdapter(23359): at com.google.android.searchcommon.utils.ThreadChanger$1$1.run(ThreadChanger.java:77)
03-03 21:16:07.461: E/ListenerAdapter(23359): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
03-03 21:16:07.461: E/ListenerAdapter(23359): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-03 21:16:07.461: E/ListenerAdapter(23359): at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)
03-03 21:16:07.461: E/ListenerAdapter(23359): at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
03-03 21:16:07.461: E/ListenerAdapter(23359): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-03 21:16:07.461: E/ListenerAdapter(23359): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-03 21:16:07.461: E/ListenerAdapter(23359): at com.google.android.searchcommon.utils.ConcurrentUtils$2$1.run(ConcurrentUtils.java:112)
还有一些时候我有这个:
03-03 21:47:13.344: E/ListenerAdapter(23359): onError
03-03 21:47:13.344: E/ListenerAdapter(23359): com.google.android.speech.exception.AudioRecognizeException: Audio error
03-03 21:47:13.344: E/ListenerAdapter(23359): at com.google.android.speech.embedded.Greco3Recognizer.read(Greco3Recognizer.java:107)
03-03 21:47:13.344: E/ListenerAdapter(23359): at dalvik.system.NativeStart.run(Native Method)
03-03 21:47:13.344: E/ListenerAdapter(23359): Caused by: java.io.IOException: couldn't start recording, state is:1
03-03 21:47:13.344: E/ListenerAdapter(23359): at com.google.android.speech.audio.MicrophoneInputStream.ensureStartedLocked(MicrophoneInputStream.java:119)
03-03 21:47:13.344: E/ListenerAdapter(23359): at com.google.android.speech.audio.MicrophoneInputStream.read(MicrophoneInputStream.java:159)
03-03 21:47:13.344: E/ListenerAdapter(23359): at com.google.common.io.ByteStreams.read(ByteStreams.java:806)
03-03 21:47:13.344: E/ListenerAdapter(23359): at com.google.android.speech.audio.Tee.readFromDelegate(Tee.java:374)
03-03 21:47:13.344: E/ListenerAdapter(23359): at com.google.android.speech.audio.Tee.readLeader(Tee.java:267)
03-03 21:47:13.344: E/ListenerAdapter(23359): at com.google.android.speech.audio.Tee$TeeLeaderInputStream.read(Tee.java:464)
03-03 21:47:13.344: E/ListenerAdapter(23359): at java.io.InputStream.read(InputStream.java:163)
03-03 21:47:13.344: E/ListenerAdapter(23359): at com.google.android.speech.audio.AudioSource$CaptureThread.run(AudioSource.java:193)
这就是我启动两者的方式:
//previously in constructor
speechrec = SpeechRecognizer.createSpeechRecognizer(getActivity());
speechrec.setRecognitionListener(this);
//
public void launchListening()
{
if (speechrec.isRecognitionAvailable(getActivity()))
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechrec.startListening(intent);
}
bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);// * bufferSizeFactor;
audio = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
audio.startRecording();
captureThread = new Thread(new Runnable()
{
public void run()
{
//calculate amplitude here
}
});
captureThread.start();
}
关于如何为语音识别创建自定义对话框的任何想法,我可以像谷歌那样根据噪声显示幅度?