Android 13 上的语音识别

问题描述 投票:0回答:1

我正在构建一个 Android 应用程序,并且正在使用语音识别,但虽然该应用程序在 Android 11 及以下任何版本上运行良好,但在 Android 12 之外的任何版本上我都遇到了障碍。尽管投入了大量时间进行故障排除,我不知道这个问题的根本原因。

我什至尝试遵循相关 Stack Overflow 帖子中提供的建议这里

不幸的是,即使遵循这些建议,结果还是一样。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

           *************************

          initSpeechRecognition(this);
        speech.setRecognitionListener(this);
        recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,                 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());

}
public void initSpeechRecognition(Activity activity) {
        String recognitionServiceName = getAvailableVoiceRecognitionService(activity);
        if (recognitionServiceName == null)
            return;

        speech = SpeechRecognizer.createSpeechRecognizer(activity,
                ComponentName.unflattenFromString(recognitionServiceName));
        Log.i("TEST", recognitionServiceName);
    }

    public static String getAvailableVoiceRecognitionService(Activity activity) {
        final List<ResolveInfo> services = activity.getPackageManager().queryIntentServices(
                new Intent(RecognitionService.SERVICE_INTERFACE), 0);

        String recognitionServiceName = null;

        for (final ResolveInfo info : services) {
            String packageName = info.serviceInfo.packageName;
            String serviceName = info.serviceInfo.name;

            String testRecognitionServiceName = packageName + "/" + serviceName;

            ServiceConnection connection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.i("TEST", name + "::" + service);
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i("TEST", name + "::Disconnected");
                }
            };

            Intent serviceIntent = new Intent(RecognitionService.SERVICE_INTERFACE);

            ComponentName recognizerServiceComponent = ComponentName.unflattenFromString(testRecognitionServiceName);

            if (recognizerServiceComponent != null) {
                serviceIntent.setComponent(recognizerServiceComponent);
                try {
                    boolean isServiceAvailableToBind = activity.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
                    if (isServiceAvailableToBind) {
                        activity.unbindService(connection);
                        recognitionServiceName = testRecognitionServiceName;
                        break;
                    }
                } catch (SecurityException e) {
                    e.printStackTrace();
                }
            }
        }

        return recognitionServiceName;
    }

这些是其余的方法

@Override
    public void onBeginningOfSpeech() {
        Log.i(VOICE_RECOGNITION, "onBeginningOfSpeech");
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        Log.i(VOICE_RECOGNITION, "onBufferReceived: " + Arrays.toString(buffer));
    }

    @Override
    public void onEndOfSpeech() {
        Log.i(VOICE_RECOGNITION, "onEndOfSpeech");
    }

    @Override
    public void onError(int errorCode) {
        String errorMessage = getErrorText(errorCode);
        Log.d(VOICE_RECOGNITION, "FAILED " + errorMessage);
    }

    @Override
    public void onEvent(int arg0, Bundle arg1) {
        Log.i(VOICE_RECOGNITION, "onEvent");
    }

    @Override
    public void onPartialResults(Bundle arg0) {
        Log.i(VOICE_RECOGNITION, "onPartialResults");
    }

    @Override
    public void onReadyForSpeech(Bundle arg0) {
        Log.i(VOICE_RECOGNITION, "onReadyForSpeech");
    }

    @Override
    public void onResults(Bundle results) {
        Log.i(VOICE_RECOGNITION, "onResults");
        ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        StringBuilder text = new StringBuilder();
        assert matches != null;
        for (String result : matches) text.append(result).append("\n");
        audioToText.add(text.toString());
        Log.i(VOICE_RECOGNITION, String.valueOf(audioToText));
    }

    @Override
    public void onRmsChanged(float rmsdB) {
        Log.i(VOICE_RECOGNITION, "onRmsChanged: " + rmsdB);
    }

    public static String getErrorText(int errorCode) {
        String message;
        switch (errorCode) {
            case SpeechRecognizer.ERROR_AUDIO:
                message = "Audio recording error";
                audioToText.add("audio recording error");
                break;
            case SpeechRecognizer.ERROR_CLIENT:
                message = "Client side error";
                audioToText.add("client side error");
                break;
            case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
                message = "Insufficient permissions";
                audioToText.add("insufficient permissions");
                break;
            case SpeechRecognizer.ERROR_NETWORK:
                message = "Network error";
                audioToText.add("network error");
                break;
            case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
                message = "Network timeout";
                audioToText.add("network timeout");
                break;
            case SpeechRecognizer.ERROR_NO_MATCH:
                message = "No match";
                audioToText.add("no audio detected");
                break;
            case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
                message = "RecognitionService busy";
                audioToText.add("recognition service busy");
                break;
            case SpeechRecognizer.ERROR_SERVER:
                message = "error from server";
                audioToText.add("error from server");
                break;
            case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
                message = "No speech input";
                audioToText.add("no speech input");
                break;
            default:
                message = "Didn't understand, please try again.";
                audioToText.add("no audio detected");
                break;
        }
        return message;
    }

我还在适当的位置放置了 startListening() 和 stopListening() 的代码,并且它们被触发得很好。

这也是我的日志部分,当应用程序开始收听语音时触发

2023-08-25 12:07:55.696 31209-31209 Voice Recognition     I  onReadyForSpeech
2023-08-25 12:07:55.730 31209-31209 Voice Recognition     I  onRmsChanged: -2.0
2023-08-25 12:07:58.091 31209-31209 Voice Recognition     D  FAILED No match

我知道该应用程序在我拥有的 Android 10 设备上运行良好,但在另一台具有 Android 13 的设备上运行良好,并且我知道谷歌在 Android 12 和 13 中的语音识别器中进行了一些更改,但没有位置互联网正确地展示了如何实现对 SpeechService 的更改并使其在 android 13 上工作,即使在官方文档网站上也没有任何代码片段来展示如何使用 android 13 实现语音识别。

任何人都可以提供有关此事的见解或建议潜在的解决方案吗?我们将非常感谢您的帮助。

java android speech-recognition speech-to-text android-13
1个回答
0
投票

我已经解决了问题! GoogleApp Android13不再支持语音识别服务,您需要安装才能获取Google的语音识别服务,您将获得一项新服务:GoogleTTSRecognitionService。

您可以查看此链接: https://developer.android.com/about/versions/13/behavior-changes-all#speech-service

main content

© www.soinside.com 2019 - 2024. All rights reserved.