SpeechRecognizer导致ANR ...我需要与Android语音API帮助 [英] SpeechRecognizer causes ANR... I need help with Android speech API

查看:229
本文介绍了SpeechRecognizer导致ANR ...我需要与Android语音API帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改的:我应该提到这一点了,但我在运行此code的服务。整个应用程序是导通/截止由一插件按钮和不具有活性。


更新的:我试图安装的SDK源的项目,所以我能得到​​那里的故障是发生更precise的想法,但是从<一个href="http://groups.google.com/group/android-developers/browse_thread/thread/a680b65f287e1b8c/caecec75f1b384f3?lnk=gst&q=+Stub!+%09&pli=1">the看起来它的,只有公共API都包括在内,这似乎使他们少了很多有用的...谁能建议至少一个调试的方法来解决这个问题?我有点卡住了。


我试图使用Android的语音识别包,以记录用户的语音和它转换成文本。不幸的是,当我尝试发起听,我得到一个ANR错误,不指向任何具体。

由于SpeechRecognizer API表示,一个RuntimeException是,如果你尝试从主线程调用它抛出。这让我怀疑如果处理实在是太苛刻......但我知道,其他应用程序使用Android API用于此目的,它通常是pretty的瞬间。

java.lang.RuntimeException的:SpeechRecognizer只应该用于从应用程序的主线程

这里是code我想从我的服务调用(修剪)样品。这是正确的做法?

感谢您抽出宝贵时间来帮助。这是一个障碍,我一直没能挺过来呢。

 意向意图=新的意图(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
        com.domain.app);

SpeechRecognizer识别= SpeechRecognizer
        .createSpeechRecognizer(this.getApplicationContext());
RecognitionListener监听器=新RecognitionListener(){
    @覆盖
    公共无效onResults(捆绑的结果){
        ArrayList的&LT;字符串&GT; voiceResults =结果
                .getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
        如果(voiceResults == NULL){
            Log.e(的getString(R.string.log_label),无声音的结果);
        } 其他 {
            Log.d(的getString(R.string.log_label),打印匹配:);
            对于(字符串匹配:voiceResults){
                Log.d(的getString(R.string.log_label),匹配);
            }
        }
    }

    @覆盖
    公共无效onReadyForSpeech(包PARAMS){
        Log.d(的getString(R.string.log_label),准备的演讲);
    }

    @覆盖
    公共无效onerror的(INT错误){
        Log.d(的getString(R.string.log_label)
                错误监听演讲:+误差);
    }

    @覆盖
    公共无效onBeginningOfSpeech(){
        Log.d(的getString(R.string.log_label),开始讲话);
    }
};
recognizer.setRecognitionListener(听众);
recognizer.startListening(意向);
 

解决方案

您应该不需要自己创建SpeechRecognizer类,也不需要实现一个RecognizerListener。谷歌让他们公开是好的,但它们看起来pretty的复杂,可能是由专家使用。

要得到用户的语音哟只需使用RecognizerIntent.ACTION_RECOGNIZE_SPEECH启动内置的语音识别活动,然后等待结果回来的onActivityResult文本。看看这个例子code在这里:

<一个href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.html" rel="nofollow">http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.html

我从这篇文章把它。

http://developer.android.com/resources/articles/speech- input.html

好运。

EDIT: I should have mentioned this already, but I'm running this code in a service. The entire app is turned on/off by a widget button and has no activity.


Update: I tried attaching the SDK sources to the project so I could get a more precise idea of where the failure was occurring, but from the looks of it, only public APIs are included, which seems to make them a lot less useful... can anyone suggest at least a debugging approach for solving this issue? I'm kind of stuck.


I'm trying to use Android's speech recognition package to record user speech and translate it to text. Unfortunately, when I attempt initiate listening, I get an ANR error that doesn't point to anything specific.

As the SpeechRecognizer API indicates, a RuntimeException is thrown if you attempt to call it from the main thread. This would make me wonder if the processing was just too demanding... but I know that other applications use the Android API for this purpose and it is typically pretty snappy.

java.lang.RuntimeException: SpeechRecognizer should be used only from the application's main thread

Here is a (trimmed) sample of the code I'm trying to call from my service. Is this the proper approach?

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

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,
        "com.domain.app");

SpeechRecognizer recognizer = SpeechRecognizer
        .createSpeechRecognizer(this.getApplicationContext());
RecognitionListener listener = new RecognitionListener() {
    @Override
    public void onResults(Bundle results) {
        ArrayList<String> voiceResults = results
                .getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
        if (voiceResults == null) {
            Log.e(getString(R.string.log_label), "No voice results");
        } else {
            Log.d(getString(R.string.log_label), "Printing matches: ");
            for (String match : voiceResults) {
                Log.d(getString(R.string.log_label), match);
            }
        }
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
        Log.d(getString(R.string.log_label), "Ready for speech");
    }

    @Override
    public void onError(int error) {
        Log.d(getString(R.string.log_label),
                "Error listening for speech: " + error);
    }

    @Override
    public void onBeginningOfSpeech() {
        Log.d(getString(R.string.log_label), "Speech starting");
    }
};
recognizer.setRecognitionListener(listener);
recognizer.startListening(intent);

解决方案

You shouldn't need to create the SpeechRecognizer class yourself, nor do you need to implement a RecognizerListener. Google made them public to be nice, but they look pretty complicated and probably for use by experts only.

To get text from the users speech yo simply need to use the RecognizerIntent.ACTION_RECOGNIZE_SPEECH to launch the built-in speech recognizer Activity and then wait for the result to come back in onActivityResult. Take a look at the example code here:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.html

I pulled it from this article.

http://developer.android.com/resources/articles/speech-input.html

Good Luck.

这篇关于SpeechRecognizer导致ANR ...我需要与Android语音API帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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