Android的SpeechRecognizer网络错误 [英] Android SpeechRecognizer Network Error

查看:754
本文介绍了Android的SpeechRecognizer网络错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android 4.4系统来创建一个连续语音识别,简单的在一个TextView显示所说的话,就像是听写。我也跟着多教程,像<一个href=\"https://github.com/fcrisciani/android-speech-recognition/blob/master/VoiceRecognition/src/com/speech/fcrisciani/voicerecognition/ContinuousDictationFragment.java\" rel=\"nofollow\">https://github.com/fcrisciani/android-speech-recognition/blob/master/VoiceRecognition/src/com/speech/fcrisciani/voicerecognition/ContinuousDictationFragment.java,或<一个href=\"http://stackoverflow.com/questions/4975443/is-there-a-way-to-use-the-speechrecognizer-api-directly-for-speech-input\">Is有没有直接使用SpeechRecognizer API将语音输入,并实施了以下版本的方法:

I'm trying to create a continous speech recognition in Android 4.4, simple displaying the spoken words in a TextView, like a dictate. I followed multiple tutorials, like https://github.com/fcrisciani/android-speech-recognition/blob/master/VoiceRecognition/src/com/speech/fcrisciani/voicerecognition/ContinuousDictationFragment.java, or Is there a way to use the SpeechRecognizer API directly for speech input? and implemented the following version:

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);
    }
}

我已经添加权限,以我的Andr​​oid清单:

I already added permissions to my Android Manifest:

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

问题是,该RecognitionListener功能,如onBeginningOfSpeech()是随机调用平均每第三次我要激活它扔按钮presses。此外,我没有收到任何效果,但错误code 2:网络错误,如果RecognitionListener被激活。尽管我也跟着教程,我是不是能够发现的差异,使我的code没有工作至今。我的LogCat中输出抛出下面的错误时,pressing按钮:

The problem is, that the RecognitionListener functions like onBeginningOfSpeech() is called randomly in average every third time I want to activate it threw button presses. Furthermore I don't receive any results, but ErrorCode 2: Network Error, if the RecognitionListener is activated. Even though I followed the tutorials, I wasn't able to spot the difference, making my code not work until now. My LogCat output throws the following errors when pressing the button:

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

不过,即使我想补充

But even if I add

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

的问题,如上所述,继续

the problems, described above, continue.

感谢您抽出宝贵时间来帮助。这一直是一道坎我一直没能得到还没有结束。

Thanks for taking the time to help. This has been a hurdle I haven't been able to get over yet.

推荐答案

据<一个href=\"http://stackoverflow.com/questions/22916633/use-of-speechrecognizer-produces-error-network-value-2\">Use的SpeechRecognizer产生ERROR_NETWORK(价值2),错误是由三星电子语音API(搭载Vlingo的)引起的。更改语音API在手机设置标准的谷歌API解决的问题。

According to Use of SpeechRecognizer produces ERROR_NETWORK (Value 2), the error was caused by Samsungs speech API (powered by Vlingo). Changing the speech API in the phone settings to standard Google API solved the problems.

这篇关于Android的SpeechRecognizer网络错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆