在android手机中,如果没有恼人的对话框,我如何使用语音识别
在不修改android API的情况下,这是可能的吗? 我找到了一篇关于这方面的文章。 有一条评论说我应该修改android API。 但它没有说明如何进行修改。 有人能给我一些建议吗? 谢谢在android手机中,如果没有恼人的对话框,我如何使用语音识别,android,speech-recognition,speech,Android,Speech Recognition,Speech,在不修改android API的情况下,这是可能的吗? 我找到了一篇关于这方面的文章。 有一条评论说我应该修改android API。 但它没有说明如何进行修改。 有人能给我一些建议吗? 谢谢 我找到了这篇文章; 他的需要和我的几乎一样。 这对我来说是一个很好的参考 我已经完全解决了这个问题。 我用谷歌搜索了一个可用的示例代码 这是我的源代码 package voice.recognition.test; import android.app.Activity; import androi
我找到了这篇文章; 他的需要和我的几乎一样。 这对我来说是一个很好的参考
我已经完全解决了这个问题。
我用谷歌搜索了一个可用的示例代码 这是我的源代码
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");
}
}
}
package voice.recognition.test;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.speech.RecognitionListener;
导入android.speech.RecognizerIntent;
导入android.SpeechRecognizer;
导入android.widget.Button;
导入android.widget.TextView;
导入java.util.ArrayList;
导入android.util.Log;
公共类voiceRecognitionTest扩展活动实现OnClickListener
{
私有文本查看多行文本;
私人语音识别器sr;
私有静态最终字符串TAG=“MyStt3Activity”;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
按钮speakButton=(按钮)findViewById(R.id.btn_speak);
mText=(TextView)findViewById(R.id.textView1);
speakButton.setOnClickListener(此);
sr=SpeechRecognizer.createSpeechRecognizer(此);
sr.setRecognitionListener(新的listener());
}
类侦听器实现识别侦听器
{
ReadyForSpeech上的公共void(Bundle参数)
{
Log.d(标签“onReadyForSpeech”);
}
开始时的公共无效fSpeech()
{
Log.d(标签“onbeginingofspeech”);
}
在RMSCHANGED上的公共无效(浮动rmsdB)
{
Log.d(标签“onRmsChanged”);
}
已接收公共无效onBufferReceived(字节[]缓冲区)
{
Log.d(标记“onBufferReceived”);
}
公共无效onEndOfSpeech()
{
Log.d(标签“onEndofSpeech”);
}
公共无效onError(内部错误)
{
日志d(标签“错误”+错误);
mText.setText(“错误”+错误);
}
公共结果(捆绑结果)
{
String str=新字符串();
Log.d(标签“onResults”+结果);
ArrayList data=results.getStringArrayList(SpeechRecognitor.results\u RECOGNITION);
对于(int i=0;i
一定要在调试后删除恼人的日志 使用界面。你的应用程序需要具有录制音频权限,然后你可以创建一个SpeechRecognitor,给它一个语音识别器,然后调用它的
startListening
方法。当语音识别器准备开始监听语音时,当它接收语音并将其转换为文本时,您将收到对侦听器的回调。有一个方便的抽象类,您可以使用它来使用语音识别器
类,只需很少的新代码。还有一个例子是使用和作为后台服务运行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);
}
});
}
我最终制作了Github项目,将文本转换为语音,并将语音转换为文本,而无需烦人的对话框,
这绝对是可能的,正如我见过其他应用程序那样(Voice infinity),但至于如何做到,我一点线索也没有。我想你可以先下载android源代码,检查语音所在的api,然后尝试扩展……正如Femi所指出的,请确保AndroidManifest.xml文件中有
,否则SpeechRecognitor将无法接收任何音频谢谢你的建议。我现在就试试,也别忘了销毁OnDestroy()方法中的SpeechRecograiner,如下所述:to not get泄露了ServiceConnection android.SpeechRecograiner$Connection@414f0e40这本来是绑定在这里的
error您能给我举个例子吗?另外,我可以在屏幕关闭时使用它吗?你介意指导我如何在Main活动中实现它们吗?这是什么意思“*使用{@link Intent}启动和停止它?”谢谢你能给我举个例子吗?另外,我可以在屏幕关闭时使用它吗?
//SPEECH TO TEXT DEMO
speechToText.setOnClickListener({ view ->
Snackbar.make(view, "Speak now, App is listening", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
TranslatorFactory
.instance
.with(TranslatorFactory.TRANSLATORS.SPEECH_TO_TEXT,
object : ConversionCallback {
override fun onSuccess(result: String) {
sttOutput.text = result
}
override fun onCompletion() {
}
override fun onErrorOccurred(errorMessage: String) {
erroConsole.text = "Speech2Text Error: $errorMessage"
}
}).initialize("Speak Now !!", this@HomeActivity)
})
//TEXT TO SPEECH DEMO
textToSpeech.setOnClickListener({ view ->
val stringToSpeak :String = ttsInput.text.toString()
if (null!=stringToSpeak && stringToSpeak.isNotEmpty()) {
TranslatorFactory
.instance
.with(TranslatorFactory.TRANSLATORS.TEXT_TO_SPEECH,
object : ConversionCallback {
override fun onSuccess(result: String) {
}
override fun onCompletion() {
}
override fun onErrorOccurred(errorMessage: String) {
erroConsole.text = "Text2Speech Error: $errorMessage"
}
})
.initialize(stringToSpeak, this)
} else {
ttsInput.setText("Invalid input")
Snackbar.make(view, "Please enter some text to speak", Snackbar.LENGTH_LONG).show()
}
})