我希望创建一个具有Speech to text的应用程序。
我使用RecognizerIntent:http://android-developers.blogspot.com/search/label/Speech%20Input
了解这种能力但是 - 我不希望弹出一个新的Intent,我想在我当前的应用程序中对某些点进行分析,我不希望它弹出一些声明它正在尝试录制你的声音
有没有人知道如何最好地做到这一点。我或许正在考虑尝试Sphinx 4 - 但我不知道这是否可以在Android上运行 - 有没有人有任何建议或经验?!
我想知道我是否可以在这里修改代码,或许不打算显示UI或按钮,只是进行处理:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.html
干杯,
答案 0 :(得分:16)
如果您不想使用RecognizerIntent
进行语音识别,您仍然可以使用SpeechRecognizer
类来执行此操作。但是,使用该类比使用intent更棘手。作为最后一点,我强烈建议让用户知道他何时被录音,否则他可能会非常安静,当他终于发现时。
编辑:一个受到启发(但已更改)的小例子, SpeechRecognizer causes ANR... I need help with Android speech API
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.domain.app");
SpeechRecognizer recognizer = SpeechRecognizer
.createSpeechRecognizer(this.getApplicationContext());
RecognitionListener listener = new RecognitionListener() {
@Override
public void onResults(Bundle results) {
ArrayList<String> voiceResults = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (voiceResults == null) {
System.out.println("No voice results");
} else {
System.out.println("Printing matches: ");
for (String match : voiceResults) {
System.out.println(match);
}
}
}
@Override
public void onReadyForSpeech(Bundle params) {
System.out.println("Ready for speech");
}
/**
* ERROR_NETWORK_TIMEOUT = 1;
* ERROR_NETWORK = 2;
* ERROR_AUDIO = 3;
* ERROR_SERVER = 4;
* ERROR_CLIENT = 5;
* ERROR_SPEECH_TIMEOUT = 6;
* ERROR_NO_MATCH = 7;
* ERROR_RECOGNIZER_BUSY = 8;
* ERROR_INSUFFICIENT_PERMISSIONS = 9;
*
* @param error code is defined in SpeechRecognizer
*/
@Override
public void onError(int error) {
System.err.println("Error listening for speech: " + error);
}
@Override
public void onBeginningOfSpeech() {
System.out.println("Speech starting");
}
@Override
public void onBufferReceived(byte[] buffer) {
// TODO Auto-generated method stub
}
@Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
}
@Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
}
@Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
}
@Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
};
recognizer.setRecognitionListener(listener);
recognizer.startListening(intent);
重要说明:从UI线程运行此代码,并确保您具有所需的权限。
<uses-permission android:name="android.permission.RECORD_AUDIO" />
答案 1 :(得分:4)
Android内置的功能(通过意图启动)是一种客户端活动,可捕获您的语音并将音频发送到Google服务器进行识别。你可以建立类似的东西。您可以自己托管狮身人面像(或使用像Yapme.com这样的云识别服务),自己捕捉声音,将音频发送到识别器,然后将文本结果返回给您的应用。我不知道如何在不使用Android(或Chrome)上的Intent的情况下利用Google识别服务。
到目前为止,我所看到的普遍共识是,今天的智能手机并不具备像Sphinx一样的语音识别能力。您可能想要自己探索运行客户端识别器,但Google使用服务器识别。
有关相关信息,请参阅:
答案 2 :(得分:2)
在您的活动中,请执行以下操作:
Image button buttonSpeak = findView....;// initialize it.
buttonSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptSpeechInput();
}
});
private void promptSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
EditText input ((EditText)findViewById(R.id.editTextTaskDescription));
input.setText(result.get(0)); // set the input data to the editText alongside if want to.
}
break;
}
}
}
答案 3 :(得分:0)
向MANIFEST添加权限:-
"android.permission.RECORD_AUDIO"
单击“按钮”(使用侦听器)时调用getSpeechInput()
public void getSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 10);
} else {
Toast.makeText(this, "Your Device Don't Support Speech Input", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 10:
if (resultCode == RESULT_OK && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Toast.makeText(this, result.get(0), Toast.LENGTH_SHORT).show();
}
break;
}
}