我怎样才能使用语音识别没有恼人的对话在Android手机

这是可能的,而无需修改Android API? 我find了一篇关于这个的文章。 有一个评论,我应该做的android API的修改。 但没有说如何做修改。 有谁能给我一些build议,如何做到这一点? 谢谢!


我find了这篇文章; SpeechRecognizer他的需求和我的几乎一样。 对我来说这是一个很好的参考!


我已经完全解决了这个问题。
我从这个中国网站search了一个可用的示例代码这里是我的源代码

package voice.recognition.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.speech.RecognitionListener; import android.speech.RecognizerIntent; import android.speech.SpeechRecognizer; import android.widget.Button; import android.widget.TextView; import java.util.ArrayList; import android.util.Log; public class voiceRecognitionTest 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); Button speakButton = (Button) findViewById(R.id.btn_speak); 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); mText.setText("error " + error); } public void onResults(Bundle results) { String str = new String(); Log.d(TAG, "onResults " + results); ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); for (int i = 0; i < data.size(); i++) { Log.d(TAG, "result " + data.get(i)); str += data.get(i); } 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.btn_speak) { 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,"voice.recognition.test"); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); sr.startListening(intent); Log.i("111111","11111111"); } } } 

debugging完成后请务必删除恼人的日志

使用SpeechRecognizer接口。 你的应用程序需要有RECORD_AUDIO权限,然后你可以创build一个SpeechRecognizer,给它一个RecognitionListener ,然后调用它的startListening方法。 当语音识别器准备开始收听语音,并接收语音并将其转换为文本时,您将获得回叫。

GAST有一个方便的抽象类,可以使用SpeechRecognizer类来使用很less的新代码。 还有一个使用这个和这个运行SpeechRecognizer作为后台服务的例子

感谢张贴这个! 我发现在oncreate中定义onclick监听器很有帮助:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mText = (TextView) findViewById(R.id.textView1); MyRecognitionListener listener = new MyRecognitionListener(); sr = SpeechRecognizer.createSpeechRecognizer(this); sr.setRecognitionListener(listener); findViewById(R.id.button1).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US"); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1); intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test"); sr.startListening(intent); } }); } 

我曾经尝试把所有的TTS和STT学习都存储在这个Github Repo中 。 如果你喜欢一个衬里,那么你可以使用我的项目。

它使用工厂模式来将语音转换成文本,而不用烦人的对话

SpeechToText(STT)

  TranslatorFactory.getInstance().getTranslator(TranslatorFactory.TRANSLATOR_TYPE.SPEECH_TO_TEXT, HomeActivity.this) .initialize("Hello There", HomeActivity.this); 

输出 : –

在这里输入图像描述

TextToSpeech(TTS)

 TranslatorFactory.getInstance().getTranslator(TranslatorFactory.TRANSLATOR_TYPE.TEXT_TO_SPEECH, HomeActivity.this) .initialize((null != message && !message ? message : "Invalid Input"), HomeActivity.this); 

输出 : –

在这里输入图像描述