简单的Android文本到语音概念证明不起作用

时间:2014-05-14 04:02:02

标签: android audio text-to-speech

我在Android上尝试使用文本到语音的概念证明但没有成功。关于这一点的任何建议,在这里我将如何进行:

这发生在片段中:

public class PhotoDetailsFragment extends Fragment implements TextToSpeech.OnInitListener{
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //initialising text to speech to speak the common names
        tts = new TextToSpeech(getActivity(), this);

    }

@Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = tts.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() {
        System.out.println("Speaking");
        tts.speak("Animal", TextToSpeech.QUEUE_FLUSH, null);
    }
..................
}

1 个答案:

答案 0 :(得分:0)

我使用tts.setLanguage(Locale.US)运气不好。这是我使用的代码:

    if (mSpeaker.isLanguageAvailable(Locale.UK) == TextToSpeech.LANG_AVAILABLE) {
        Log.i(TAG, "Whee!  We have UK English");
        mSpeaker.setLanguage(Locale.UK);

    } else if (mSpeaker.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE) {
        Log.i(TAG, "We have US English");
        mSpeaker.setLanguage(Locale.US);
    } else {
        Log.i(TAG, "Using Speech engine default Locale");

    }

当此代码在我的Samsung S4手机上运行时,它使用默认语言环境而不是Locale.UK或Locale.US。

尝试删除tts.setLanguage(Locale.US)和结果测试。

另一种可能性是必须从UI线程调用tts.speak()(我只是猜测 - 我不确定)。我注意到许多Android API没有指定必须从UI线程调用的内容。您可以尝试以下代码而不是调用speakOut():

    getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
                speakOut();

        }

    });