【问题标题】:Android SpeechRecognizer Network ErrorAndroid SpeechRecognizer 网络错误
【发布时间】:2014-08-25 13:18:59
【问题描述】:

我正在尝试在 Android 4.4 中创建连续语音识别,简单地在 TextView 中显示口语,就像听写一样。我遵循了多个教程,例如https://github.com/fcrisciani/android-speech-recognition/blob/master/VoiceRecognition/src/com/speech/fcrisciani/voicerecognition/ContinuousDictationFragment.javaIs 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 清单添加了权限:

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

问题是,像 onBeginningOfSpeech() 这样的 RecognitionListener 函数平均每三次随机调用一次,我想激活它会抛出按钮按下。此外,如果激活了 RecognitionListener,我没有收到任何结果,但 ErrorCode 2: Network Error。即使我按照教程进行操作,我也无法发现其中的差异,导致我的代码直到现在才起作用。按下按钮时,我的 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"/>

上述问题继续。

感谢您抽出宝贵时间提供帮助。这一直是我无法克服的障碍。

【问题讨论】:

  • 请查看this可能对您有帮助
  • 感谢您的快速回复!我可以通过将手机设置中的语音 API 从三星(由 Vlingo 提供支持)更改为 Google 来修复它,以响应此线程:stackoverflow.com/questions/22916633/…
  • 很高兴知道这一点,并做一件事创建一个答案,你是如何解决这个问题并在下面发布的,所以它会在未来帮助其他人。谢谢。

标签: android speech-to-text


【解决方案1】:

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

【讨论】:

  • 所提供的链接与“三星语音 API(由 Vlingo 提供支持)”无关?
  • 我正在使用 pocketsphinx 来控制麦克风的唤醒命令。它在删除pocketsphinx 库后开始工作。
【解决方案2】:

对于可能遇到此问题的其他人,由于同时激活了重叠的 MediaRecorder 或 AudioRecord 实例,我遇到了带有 SpeechRecognizer 的 NetworkError。它似乎在我正在开发的联想 Tab 2 A10 上运行良好,但在我测试的任何移动设备上都没有。

搜索 SpeechRecognizer 和 NetworkError 无效。

如果您遇到此问题,您可能会发现此问题很有用(或开始搜索 SpeechRecognizer 和 AudioRecord/MediaRecorder)...

Android speech recognizing and audio recording in the same time

【讨论】:

    【解决方案3】:

    我不断看到网络错误。禁用的三星 Auto Bixby 醒来并发出嘘声!问题解决了!

    【讨论】:

      猜你喜欢
      • 2015-03-15
      • 2018-07-12
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多