【问题标题】:Android text to SpeechAndroid 文字转语音
【发布时间】:2017-04-14 06:08:43
【问题描述】:

我正在尝试使用 TextToSpeech 类在我的应用程序中说文本。当我运行我的代码时,我什么也没听到,音量很高。我的代码有什么问题?我需要许可还是什么?

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener  {

    TextToSpeech textToSpeech;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textToSpeech = new TextToSpeech(this, this);

        speakOut();
    }

    @Override
    public void onInit(int Text2SpeechCurrentStatus) {

        if (Text2SpeechCurrentStatus == TextToSpeech.SUCCESS) {

            int result = textToSpeech.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }
    }

    private void speakOut() {
        String g= "Hello";
        textToSpeech.speak(g, TextToSpeech.QUEUE_FLUSH, null);
    }
}

【问题讨论】:

    标签: android android-studio android-activity android-speech-api


    【解决方案1】:

    首先要做的是:检查您的设备中是否安装了任何 TTS 引擎。

    不,您不需要任何权限即可使用 TTS。

    像这样在活动的 onCreate() 方法中初始化 TextToSpeech 实例。

    TextToSpeech t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
         @Override
         public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
               t1.setLanguage(Locale.UK);
            }
         }
      });
    

    // 这是你的speakOut() 方法。

       private void speakOut() {
        String g= "Hello";
         t1.speak(g, TextToSpeech.QUEUE_FLUSH, null);
    }
    

    希望对你有帮助...

    【讨论】:

      【解决方案2】:

      这可能是评论,我不确定,但你可以试试 改变
      if (Text2SpeechCurrentStatus == TextToSpeech.SUCCESS) {
      进入
      if (Text2SpeechCurrentStatus != TextToSpeech.ERROR) {

      也许你可以调试一下,Text2SpeechCurrentStatus的值是多少

      【讨论】:

        猜你喜欢
        • 2011-03-04
        • 1970-01-01
        • 2012-04-06
        • 1970-01-01
        • 1970-01-01
        • 2015-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多