【问题标题】:Android studio TTS: Not reading text passed into speak() functionAndroid Studio TTS:不读取传递给 speak() 函数的文本
【发布时间】:2022-01-18 17:24:18
【问题描述】:

我正在开发一个应用程序,它使用 MLKit 的 OCR 功能从图像中读取文本,将其显示给用户,然后使用 Android 的 TTS,但它似乎无法正常工作。

        private TextToSpeech textReader; // Instance of Android's built in TTS
        private String multipleBlockText; // Empty to store multiple blocks

        // Initialise TTS
        textReader = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                textReader.setLanguage(Locale.UK); // Sets language to US, English.
            }
        });

        // OCR code goes here
        
        @Override
            public void onClick(View v) {
                try {
                    detectText(); // Calls function for OCR from LKit library
                    // Reads text
                    textReader.speak(text, TextToSpeech.QUEUE_FLUSH, null);
                } catch(Exception error){ // Error handling: Prevents app crash on no text readable
                    detectedText.setText("Error!");
                }
            }
        

我也在我的清单中声明了 TTS:

 <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.TTS_SERVICE" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

但是当我运行我的应用程序时,它会向用户显示图像文本,但 TTS 不会大声朗读它。 OCR 功能工作正常,图像中的文本存储在“multipleBlockText”变量中。

【问题讨论】:

    标签: java android android-studio text-to-speech


    【解决方案1】:

    解决了!:

    与其尝试在detectText() 之后调用TTS.speak() 函数,不如在detectText 函数中调用它,如下所示:

    // in function detectText()
    ...
        for (Text.TextBlock block: text.getTextBlocks()) { // For loop for each paragraph
            String blockText = block.getText(); // Local scope store for text detected
            multipleBlockText = multipleBlockText + blockText + " "; // Concat blocks to one string
        }
    
        detectedText.setText(multipleBlockText); // Displays text to user
        textReader.speak(multipleBlockText, TextToSpeech.QUEUE_FLUSH, null);
    }
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 2020-04-21
      • 2014-12-25
      • 2020-11-04
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多