java - 语音识别器声音

标签 java android audio android-intent speech-recognition

我正在使用语音识别器意图来接受用户输入并将其转换为文本。但是,我希望Intent不断接受用户输入并将其转换为文本,以查看用户是否说了某个单词。我的代码可以做到这一点,但是每当我的应用开始监听输入时,手机都会发出一声短促的哔哔声,表明可以开始输入了。

我想知道每次识别器准备再次收听时,是否有任何方法可以从播放中删除声音。

这是我的代码:

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;


public class MainActivity extends Activity implements OnClickListener 
{

       //private TextView mText;
       private SpeechRecognizer sr;
       private static final String TAG = "MyStt3Activity";
       @Override
       public void onCreate(Bundle savedInstanceState) 
       {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                ToggleButton speakButton = (ToggleButton) findViewById(R.id.CarMode);     
                //mText = (TextView) findViewById(R.id.textView1);     
                speakButton.setOnClickListener(this);
                sr = SpeechRecognizer.createSpeechRecognizer(this);       
                sr.setRecognitionListener(new listener());        
       }

       class listener implements RecognitionListener          
       {
                public void onReadyForSpeech(Bundle params)
                {
                         Log.d(TAG, "onReadyForSpeech");
                }
                public void onBeginningOfSpeech()
                {
                         Log.d(TAG, "onBeginningOfSpeech");
                }
                public void onRmsChanged(float rmsdB)
                {
                         Log.d(TAG, "onRmsChanged");
                }
                public void onBufferReceived(byte[] buffer)
                {
                         Log.d(TAG, "onBufferReceived");
                }
                public void onEndOfSpeech()
                {
                         Log.d(TAG, "onEndofSpeech");
                }
                public void onError(int error)
                {
                         Log.d(TAG,  "error " +  error);
                         Toast.makeText(getApplicationContext(), "What was that?", Toast.LENGTH_SHORT).show();
                         sr.cancel();
                         speechIntent();
                }
                public void onResults(Bundle results)                   
                {
                         //String str = new String();
                         Log.d(TAG, "onResults " + results);
                         ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                         Toast.makeText(getApplicationContext(), data.get(0), Toast.LENGTH_SHORT).show();
                         sr.cancel();
                         speechIntent();
                         //mText.setText("results: "+String.valueOf(data.size()));        
                }
                public void onPartialResults(Bundle partialResults)
                {
                         Log.d(TAG, "onPartialResults");
                }
                public void onEvent(int eventType, Bundle params)
                {
                         Log.d(TAG, "onEvent " + eventType);
                }
       }
       public void onClick(View v) {
                if (v.getId() == R.id.CarMode) 
                {
                    speechIntent();
                }
       }

       public void speechIntent()
       {
           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,"com.example.freestyleandroid");

                    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
                         sr.startListening(intent);
                         Log.i("111111","11111111");
       }
    }

最佳答案

这应该为您解决问题。将此添加到您的RecognitionListener.onReadyForSpeech()方法。

mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);

关于java - 语音识别器声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18691037/

相关文章:

java - HTC Dialer 从电话号码中删除字母

java - 从 AsyncTask 触发时 SQLiteOpenHelper.getWritableDatabase 上出现 NullPointerException

android - 显示下拉列表时微调器的状态是什么?

android - 当用户使用“主页”或“概述”按钮离开应用程序时,如何更新 React Native 中的组件?

.net - .net mcisendstring-使用资源而不是完整路径

audio - DirectShow图形将Avi和Wav分开(播放,不保存)?

batch-file - 批处理命令停止隐藏的音轨

java - 定时器:周期小于一毫秒

java - Hibernate 无法在具有多个数据源的 Spring 项目中实例化 id 生成器

android - 从 RecyclerView 中检索每行的多个编辑文本