gpt4 book ai didi

Android SpeechRecognizer 网络错误

转载 作者:太空狗 更新时间:2023-10-29 13:23:49 27 4
gpt4 key购买 nike

我正在尝试在 Android 4.4 中创建连续语音识别,简单地在 TextView 中显示口语,就像命令一样。我遵循了多个教程,例如 https://github.com/fcrisciani/android-speech-recognition/blob/master/VoiceRecognition/src/com/speech/fcrisciani/voicerecognition/ContinuousDictationFragment.java , 或 Is there a way to use the SpeechRecognizer API directly for speech input?并实现了以下版本:

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class VoiceReadActivity extends Activity {
private ImageButton mButtonSpeech;
private TextView mTextView;

private SpeechRecognizer mSpeechRecognizer = null;

private SpeechRecognizer getSpeechRecognizer() {
if (mSpeechRecognizer == null) {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
mButtonSpeech.getBackground().setColorFilter( 0xFFFF0000, PorterDuff.Mode.MULTIPLY );
}

@Override
public void onBeginningOfSpeech() {
mTextView.append( "BEGINNING TO HEAR..." );

mButtonSpeech.getBackground().setColorFilter( 0xFFFFFFFF, PorterDuff.Mode.MULTIPLY );
}

@Override
public void onBufferReceived(byte[] buffer) {}

@Override
public void onEndOfSpeech() {
mTextView.append( "STOP HEARING..." );

mButtonSpeech.getBackground().setColorFilter( 0xFF00FFFF, PorterDuff.Mode.DST );
}

@Override
public void onError(int error) {
String message;
boolean restart = true;
switch (error)
{
case SpeechRecognizer.ERROR_AUDIO:
message = "Audio recording error";
break;
case SpeechRecognizer.ERROR_CLIENT:
message = "Client side error";
restart = false;
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
message = "Insufficient permissions";
restart = false;
break;
case SpeechRecognizer.ERROR_NETWORK:
message = "Network error";
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
message = "Network timeout";
break;
case SpeechRecognizer.ERROR_NO_MATCH:
message = "No match";
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
message = "RecognitionService busy";
break;
case SpeechRecognizer.ERROR_SERVER:
message = "error from server";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
message = "No speech input";
break;
default:
message = "Not recognised";
break;
}
mTextView.append("onError code:" + error + " message: " + mes sage);

if (restart) {
getSpeechRecognizer().cancel();
startVoiceRead();
}
}

@Override
public void onEvent(int eventType, Bundle params) {}

@Override
public void onPartialResults(Bundle partialResults) {}

@Override
public void onResults(Bundle results) {
ArrayList<String> text = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
mTextView.append(text.get(0));
mTextView.append( ". " );

startVoiceRead();
}

@Override
public void onRmsChanged(float rmsdB) {}
});
}
return mSpeechRecognizer;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mButtonSpeech = (ImageButton)findViewById(R.id.button_speech);
mTextView = (TextView)findViewById(R.id.text_slave);

mButtonSpeech.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mSpeechRecognizer == null) {
startVoiceRead();
} else {
stopVoiceRead();
}
}
});
}

public void startVoiceRead() {
Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
getSpeechRecognizer().startListening(speechIntent);

mButtonSpeech.getBackground().setColorFilter( 0xFFFFFF00, PorterDuff.Mode.MULTIPLY );
}

public void stopVoiceRead() {
if (mSpeechRecognizer != null) {
mSpeechRecognizer.destroy();
mSpeechRecognizer = null;
}

mButtonSpeech.getBackground().setColorFilter( 0xFFFFFFFF, PorterDuff.Mode.DST);
}
}

我已经在我的 Android Manifest 中添加了权限:

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET"/>

问题是,像 onBeginningOfSpeech() 这样的 RecognitionListener 函数平均每三次被随机调用,我想激活它会引发按钮按下。此外,如果 RecognitionListener 已激活,我没有收到任何结果,但收到错误代码 2:网络错误。即使我按照教程进行操作,我也无法发现差异,导致我的代码直到现在才起作用。按下按钮时,我的 LogCat 输出会抛出以下错误:

07-04 10:20:22.714: E/DatabaseUtils(869): Writing exception to parcel
07-04 10:20:22.714: E/DatabaseUtils(869): java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
07-04 10:20:22.714: E/DatabaseUtils(869): at com.android.server.am.ActivityManagerService.handleIncomingUser(ActivityManagerService.java:14614)
07-04 10:20:22.714: E/DatabaseUtils(869): at android.app.ActivityManager.handleIncomingUser(ActivityManager.java:2258)
07-04 10:20:22.714: E/DatabaseUtils(869): at com.android.providers.settings.SettingsProvider.call(SettingsProvider.java:663)
07-04 10:20:22.714: E/DatabaseUtils(869): at android.content.ContentProvider$Transport.call(ContentProvider.java:325)
07-04 10:20:22.714: E/DatabaseUtils(869): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:275)
07-04 10:20:22.714: E/DatabaseUtils(869): at android.os.Binder.execTransact(Binder.java:404)
07-04 10:20:22.714: E/DatabaseUtils(869): at dalvik.system.NativeStart.run(Native Method)
07-04 10:20:22.714: W/ActivityManager(869): Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
07-04 10:20:22.734: E/VoiceEngineWrapper(16366): getInstance() : get existed VoiceEngine
07-04 10:20:22.734: E/VSG(16366): VSG: speechEndpointTimeout = 1500
07-04 10:20:22.734: E/VSG(16366): VSG: DEFAULT_ENDPOINT_MEDIUM is 1750
07-04 10:20:22.734: E/VSG(16366): VSG: Not using dynamic HANGOVER ... speechEndpointTimeout is 1500
07-04 10:20:22.734: E/VSG(16366): VSG: SHORT = 400
07-04 10:20:22.734: E/VSG(16366): VSG: MEDIUM = 750
07-04 10:20:22.734: E/VSG(16366): VSG: MEDIUM_LONG = 1250
07-04 10:20:22.734: E/VSG(16366): VSG: LONG = 1750
07-04 10:20:22.734: E/VSG(16366): VSG: LONG_LONG = 2250

但即使我添加

<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL"/>

上述问题仍在继续。

感谢您花时间提供帮助。这是我至今无法克服的障碍。

最佳答案

根据 Use of SpeechRecognizer produces ERROR_NETWORK (Value 2) ,错误是由三星的语音API(由Vlingo提供支持)引起的。将手机设置中的语音 API 更改为标准 Google API 解决了问题。

关于Android SpeechRecognizer 网络错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24570261/

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