Android语音识别API在Android 7 Nougat中不起作用 [英] Android Speech Recognition API does not work in Android 7 Nougat

查看:184
本文介绍了Android语音识别API在Android 7 Nougat中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用android.speech.SpeechRecognizer API进行演讲。

I am using the android.speech.SpeechRecognizer API for speech.

我在Android 4-5中效果很好,

I works great in Android 4-5,

在Android 6中它现在有一堆错误,比如麦克风打开时发出的响铃被检测为语音,所以它存在(并且当它重新启动时无限循环,因为检测到语音,我们有一个hack解决方法,在播放铃声之前将音量设置为0 ...)

In Android 6 it now has a bunch of bugs, like the chime that occurs when the mic turns on is detected as speech, so it exists (and loops indefinitely when it restarts because to speech was detected, we have a hack workaround for this that sets the volume to 0 before the chime is played...)

在Android 6中,语音也会在5秒后死亡,没有任何错误或任何内容。
我们有另一个黑客解决方法,它可以检测到没有活动5秒并重新启动它...

In Android 6 the speech also dies with no error or anything after 5 seconds. We have another hack workaround for this that detects no activity for 5 seconds and restarts it...

现在在Android 7中,语音识别不会出现工作吗?
我还没有能够调试原因,但有没有人在Android 7中使用语音API时遇到问题?

Now in Android 7, the speech recognition does not appear to work at all? I have not been able to debug why as of yet, but has anyone had issues getting the speech API to work in Android 7?

此外,如果有人知道为什么Android似乎在每个版本的语音API中添加新的错误而不修复它们,请回复。
这是Android应该支持的内容,还是他们希望您使用Google意图?

Also, if anyone knows why Android seems to be adding new bugs in the speech API each release and not fixing them, please reply as well. Is this something that should be supported in Android, or do they want you to use the Google intent instead?

推荐答案

我的代码在Nexus5x(Nougat)和Nexus9(Nougat)上工作正常

My code works fine on Nexus5x(Nougat) and Nexus9(Nougat)

尝试并显示logcat。

try and show logcat.

SpeechRecognizer mGoogleSr;

void initGoogleSr(Context context) {
    mGoogleSr = SpeechRecognizer.createSpeechRecognizer(context);
    mGoogleSr.setRecognitionListener(new GoogleSrListener());
}

void startGoogleSr() {
    if (mGoogleSr != null) {
        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, getPackageName());
        intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
        mGoogleSr.startListening(intent);
    }
}
void cancelRecognizing() {
    if (mGoogleSr != null) {
        mGoogleSr.cancel();
    }
}

public class GoogleSrListener implements RecognitionListener {
    String lastPartialText;

    @Override
    public void onReadyForSpeech(Bundle params) {
        Log.v(TAG, ">>> onReadyForSpeech");
        showMessage("ready");
    }

    @Override
    public void onBeginningOfSpeech() {
        Log.v(TAG, ">>> onBeginningOfSpeech");
        showMessage("recognizing");
    }

    @Override
    public void onRmsChanged(float rmsdB) {
    }

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

    }

    @Override
    public void onEndOfSpeech() {
        Log.v(TAG, ">>> onEndOfSpeech");
        showMessage("waiting result");
    }

    @Override
    public void onError(int error) {
        Log.v(TAG, ">>> onError : " + error);
        switch (error) {
            case SpeechRecognizer.ERROR_AUDIO:
                Log.e(TAG, "ERROR_AUDIO");
                break;
            case SpeechRecognizer.ERROR_CLIENT:
                Log.e(TAG, "ERROR_CLIENT");
                break;
            case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
                Log.e(TAG, "ERROR_INSUFFICIENT_PERMISSIONS");
                break;
            case SpeechRecognizer.ERROR_NETWORK:
                Log.e(TAG, "ERROR_NETWORK");
                break;
            case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
                Log.e(TAG, "ERROR_NETWORK_TIMEOUT");
                break;
            case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
                Log.e(TAG, "ERROR_RECOGNIZER_BUSY");
                break;
            case SpeechRecognizer.ERROR_SERVER:
                Log.e(TAG, "ERROR_SERVER");
                break;
            case SpeechRecognizer.ERROR_NO_MATCH:
                Log.v(TAG, "ERROR_NO_MATCH");
                break;
            case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
                Log.v(TAG, "ERROR_SPEECH_TIMEOUT");
                break;
            default:
                Log.v(TAG, "ERROR_UNKOWN");
        }
    }

    @Override
    public void onPartialResults(Bundle partialResults) {
        Log.v(TAG, ">>> onPartialResults");
        List<String> resultList = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        if (resultList != null) {
            String text = resultList.get(0);
            if (text.equals(lastPartialText)) {
                return;
            }
            lastPartialText = text;
            Log.v(TAG, "partial : " + text);
        }
    }

    @Override
    public void onResults(Bundle results) {
        Log.v(TAG, ">>> onResults");
        List<String> resultList = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        if (resultList != null) {
            String text = resultList.get(0);
            Log.v(TAG, "result : " + text);
            showMessage(text);
        }
    }

    @Override
    public void onEvent(int eventType, Bundle params) {
        Log.v(TAG, ">>> onEvent type = " + eventType);
    }
}

清单中的权限(可能是多余的):

permissions in manifest(maybe redundant):

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

这篇关于Android语音识别API在Android 7 Nougat中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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