Android Studio 从图像跟踪语音在文本中的位置

mzillmmw  于 11个月前  发布在  Android
关注(0)|答案(1)|浏览(93)
private void recognizedTextFromImage() {
        Log.d(TAG, "recognizedTextFromImage: ");
        try {
            InputImage inputImage = InputImage.fromFilePath(this, imageUri);

            textRecognizer.process(inputImage)
                    .addOnSuccessListener(new com.google.android.gms.tasks.OnSuccessListener<Text>() {
                        @Override
                        public void onSuccess(Text text) {
                            String recognizedText = text.getText();
                            recognizedTextEt.setText(recognizedText);

                            // Speak the recognized text
                            spokenText = recognizedText;
                            speakRecognizedText(recognizedText);
                        }
                    })
                    .addOnFailureListener(new com.google.android.gms.tasks.OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.e(TAG, "onFailure: ", e);
                            Toast.makeText(MainActivity.this, "Failed recognizing text due to " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
        } catch (Exception e) {
            Log.e(TAG, "recognizedTextFromImage: ", e);
            Toast.makeText(MainActivity.this, "Failed Preparing Image due to " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    private void speakRecognizedText(String text) {
        // Check if Text-to-Speech is ready
        if (textToSpeech != null && textToSpeech.getEngines().size() > 0) {
            // Use a handler to post delayed speech synthesis
            final Handler handler = new Handler(Looper.getMainLooper());
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // Speak the recognized text out loud
                    textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, text);
                    isSpeaking = true;
                    isPaused = false;
                    startStopContinueSpeechBtn.setText("Stop");
                }
            }, 100); // Delay before starting speech
        }
    }

    private void stopSpeech() {
        if (textToSpeech != null && isSpeaking) {
            textToSpeech.stop();
            isSpeaking = false;
            isPaused = false;
            startStopContinueSpeechBtn.setText("Continue");
        }
    }

    private void continueSpeech() {
        if (textToSpeech != null && !isPaused) {
            isPaused = true;
            lastPosition = spokenText.length() - lastPosition;
            String text = spokenText.substring(lastPosition);
            textToSpeech.speak(text, TextToSpeech.QUEUE_ADD, null, text);
            isSpeaking = true;
            startStopContinueSpeechBtn.setText("Stop");
        }
    }

    private void startSpeech() {
        if (textToSpeech != null) {
            String text = recognizedTextEt.getText().toString();
            if (!text.isEmpty()) {
                spokenText = text;
                textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, text);
                isSpeaking = true;
                isPaused = false;
                startStopContinueSpeechBtn.setText("Stop");
            }
        }
    }

    @Override
    protected void onDestroy() {
        if (textToSpeech != null) {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        super.onDestroy();
    }
}

字符串
在上面的代码片段中,我已经给出了一个从图像中识别文本的函数,然后另一个函数创建了一个停止,开始和继续函数。我想让它在用户点击停止时,然后继续,它将从语音停止的地方开始。有什么方法可以做到这一点吗?我知道我需要跟踪语音在哪里,但我怎么做呢?谢谢

wko9yo5t

wko9yo5t1#

正如错误消息所说:fromFilePath(..., ...)的第一个参数应该是context,但在程序中它是OnClickListener。将this替换为getApplicationContext()

相关问题