如何获得音频幅度与语音识别? [英] How to get audio amplitude with speech recognizer?

查看:141
本文介绍了如何获得音频幅度与语音识别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义对话框,而语音识别,而不是使用官方之一。我得到的一部分,但是当后来我决定以示同时也认识到声音的振幅,从而使其更花哨,像谷歌现在的搜索栏做(它的一个圆,如果是成长麦克风的周围声音的响亮):

I'm trying to make a custom dialog while speech recognizing instead of using the official one. I got that part, but when then I decided to show the amplitude of the sound while recognizing, in order to make it more fancy, like Google Now search bar does (its a circle around the microphone that grows if voice its louder):

然后,我开始code如何获取声音的振幅,最后我得到了它与AudioRecord类。

Then I started to code how to obtain the amplitude of the sound, and finally I got it with AudioRecord Class.

的问题是当我尝试混合两者(SpeechRecognizer和AudioRecord),因为好像他们不能够共享麦克风或类似的东西...

The problem comes when I try to mix both (SpeechRecognizer and AudioRecord), because seems like they are not able to share microphone, or something like that...

在logcat中我有这样的错误:

In logcat I have this error:

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)

和其他一些时候,我有这样的:

and some other times i have this:

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)

这是我如何启动这两个:

And this is how i launch both:

//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();
}

任何关于如何创建语音识别,在那里我可以显示基于噪声振幅自定义对话框的想法,像谷歌呢?

Any ideas on how creating a custom dialog for speech recognition, where I can show amplitude based on the noise, like Google does?

推荐答案

做到这一点的方法是注册一个监听器的 SpeechRecognizer 和可视化的输出<一个href="http://developer.android.com/reference/android/speech/RecognitionListener.html#onRmsChanged%28float%29"相对=nofollow> onRmsChanged 。然而,需要注意的是:

The way to do it is to register a listener with the SpeechRecognizer and visualize the output of onRmsChanged. Note however that:

没有保证,这种方法将被调用。

There is no guarantee that this method will be called.

所以,你正在使用的需求,以支持此方法的语音识别。需要注意的是返回值 SpeechRecognizer.createSpeechRecognizer(getActivity())取决于用户的设备的配置。

So the speech recognizer that you are using needs to support this method. Note that the return value of SpeechRecognizer.createSpeechRecognizer(getActivity()) depends on the user's device's configuration.

(不能启动 AudioRecord ,而 SpeechRecognizer 正在录制,反之亦然。)

(You cannot start an AudioRecord while the SpeechRecognizer is recording and vice versa.)

这篇关于如何获得音频幅度与语音识别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆