文本到语音转换工作不正常 [英] Text to Speech not working as expected

查看:166
本文介绍了文本到语音转换工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟几个教程,但我遇到了同样的问题。首先,这里是我的简单的code:

I have followed several tutorials, but I am experiencing the same problem. First, here is my simple code:

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中这样做的原因是显而易见的。出于某种原因,他们的阅读器的初始化之前被称为已完成。所以我这个工作的权利的唯一途径是通过将说话()初始化后立即功能是一定要完成自己的方法中。

Now, notice the first speak in onCreate() which I have commented out, and the second in onStart() which I have also commented out. The reason for this is obvious based on what I receive in LogCat. For some reason, they are called before the initialization of the reader is complete. So the only way I have this working right is by placing the speak() function immediately after the initialization is sure to complete inside its own method.

所以我在想,如果有任何的方式来等待初始化完成,然后运行说话()的onCreate ONSTART()

So I was wondering if there is any way to wait for the initialization to complete, and then run speak() in onCreate or onStart().

推荐答案

请看一看,以<一href="http://www.jameselsey.co.uk/blogs/techblog/android-a-really-easy-tutorial-on-how-to-use-text-to-speech-tts-and-how-you-can-enter-text-and-have-it-spoken/"相对=nofollow>本教程。
基本上,它迫使的onCreate()方法,在初始。

please take a look to this tutorial.
Basically, it forces the init during the onCreate() method.

// 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!
米尔顿

Then, you will be able to speech any text you want at the start (with out any interaction from the user). And of course, speak will work.

HTH!
Milton

这篇关于文本到语音转换工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆