gpt4 book ai didi

android - SpeechRecognizer 未启动

转载 作者:行者123 更新时间:2023-11-29 21:08:46 27 4
gpt4 key购买 nike

简介

我需要在我的代码中实现语音识别。我按照这里的其他帖子和一些教程来获取它,但它不适合我。

方法

这是在 onCreate 中初始化它的代码:

Log.d("SPEECH", "speech recognition available: " + SpeechRecognizer.isRecognitionAvailable(this));
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(mRecognitionListener);

mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());

该 Activity 实现了 TextToSpeech.OnInitListener,因此,当此实现在它自己的方法中初始化时,我调用主要方法:

@Override
public void onInit(int status) {
myMethod();
}

然后,在 myMethod() 中,我像这样开始语音识别:

mSpeechRecognizer.startListening(mSpeechRecognizerIntent);

最后,这是结果的监听器:

private final RecognitionListener mRecognitionListener = new RecognitionListener() {
@Override
public void onBufferReceived(byte[] buffer) {
Log.d("SPEECH", "onBufferReceived");
}
@Override
public void onError(int error) {
Log.d("SPEECH", "onError: " + error);

mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.d("SPEECH", "onEvent");
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.d("SPEECH", "onPartialResults");
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.d("SPEECH", "onReadyForSpeech");
}
@Override
public void onResults(Bundle results) {
Log.d("SPEECH", "onResult");

matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
}
@Override
public void onRmsChanged(float rmsdB) {
Log.d("SPEECH", "onRmsChanged");
}
@Override
public void onBeginningOfSpeech() {
Log.d("SPEECH", "onBeginningOfSpeech");
}
@Override
public void onEndOfSpeech() {
Log.d("SPEECH", "onEndOfSpeech");
}
};

当我执行 mSpeechRecognizer.startListening(mSpeechRecognizerIntent); 时,它在 logcat 中没有显示任何错误或任何错误,但监听器未初始化,我没有在LogCat,所以我认为它没有很好地初始化。

也许我没有很好地开始听众或者可能发生了什么?

更新-- Activity 结构

public class GameActivity extends Activity implements TextToSpeech.OnInitListener {

@Override
public void onCreate(Bundle savedInstanceState) {

tts = new TextToSpeech(this, this);

Log.d("SPEECH", "speech recognition available: " + SpeechRecognizer.isRecognitionAvailable(this));
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechListener());

mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());

mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}

/*Method implemented by texttospeech*/
@Override
public void onInit(int status) {

if (status == TextToSpeech.SUCCESS) {
/*set Language*/
tts.setLanguage(Locale.getDefault());

/*STARTS MAIN METHOD*/
SpeechWhenMotion();
} else {
Log.e("TTS", "Initilization Failed");
}
}

/*Main method, does all the work*/
public void SpeechWhenMotion() {
}

最佳答案

根据documentation语音识别器必须从主线程调用。您正在尝试从 TTS 引擎的 onInit 回调中启动识别器。这不是主线程,tts 引擎回调是在单独的线程中执行的。

您需要在主线程中运行ASR初始化程序,您可以先在onCreate()方法中初始化语音识别器,然后初始化文本到语音。

或者,您可以将处理程序发布到 TTS 线程中的初始化 ASR:

handler.postDelayed(new Runnable() {
@Override
run() {
MyASRInit()
}
}

关于android - SpeechRecognizer 未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23626448/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com