【问题标题】:Text to Speech not working as expected文字转语音未按预期工作
【发布时间】:2013-01-11 20:30:08
【问题描述】:

我已经学习了几个教程,但我遇到了同样的问题。首先,这是我的简单代码:

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;

public class AchievementsActivity extends Activity implements OnInitListener {

    TextToSpeech reader;
    Locale canada;
    boolean readerInit = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


        canada = Locale.ENGLISH;
        reader = new TextToSpeech(this, this);

        //speak();

        //      while (reader.isSpeaking()) {} //waiting for reader to finish speaking

    }

    @Override
    public void onStart()   {
        super.onStart();
        //speak();

    }

    @Override
    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {
            reader.setLanguage(canada);
            reader.setPitch(0.9f);
            Log.e("Init", "Success");
            readerInit = true;
            speak();
        }

        else
            System.out.println("Something went wrong.");
    }

    public void speak() {
        reader.speak("You currently have no achievements.", TextToSpeech.QUEUE_FLUSH, null);
    }
}

现在,请注意onCreate() 中我已注释掉的第一个发言,以及onStart() 中我也已注释掉的第二个发言。根据我在 LogCat 中收到的内容,原因很明显。由于某种原因,它们在reader 的初始化完成之前被调用。因此,我拥有这项工作权利的唯一方法是在初始化后立即放置 speak() 函数,确保在它自己的方法中完成。

所以我想知道是否有什么方法可以等待初始化完成,然后在onCreateonStart() 中运行speak()

【问题讨论】:

    标签: android text-to-speech


    【解决方案1】:

    你可以这样做:

    @Override
    public void onInit(int status) {
    
        if (status == TextToSpeech.SUCCESS) {
            reader.setLanguage(canada);
            reader.setPitch(0.9f);
            Log.e("Init", "Success");
            readerInit = true;
    
            // wait a little for the initialization to complete
            Handler h = new Handler();
            h.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // run your code here
                    speak();
                }
            }, 400);
    
        }
    
        else {
            System.out.println("Something went wrong.");
        }
    
    }
    

    这不是很好,但它有效。我希望有人能找到更好的解决方案...

    【讨论】:

      【解决方案2】:

      请查看this tutorial
      基本上,它在onCreate() 方法期间强制初始化。

      // Fire off an intent to check if a TTS engine is installed
      Intent checkIntent = new Intent();
      checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
      startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
      

      然后,您将能够在开始时说出您想要的任何文本(无需用户进行任何交互)。当然,说话会起作用。

      HTH!
      米尔顿

      【讨论】:

        【解决方案3】:

        尝试为使用给定 TTS 引擎的 TextToSpeech 类使用另一个构造函数:

        TextToSpeech(this,this,"com.google.android.tts");
        

        代替:

        new TextToSpeech(this, this);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-03-05
          • 2017-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-16
          相关资源
          最近更新 更多