Android speech - how can you read text in Android?

冷暖自知 提交于 2019-12-30 07:23:44

问题


How can you read data, i.e. convert simple text strings to voice (speech) in Android?

Is there an API where I can do something like this:

TextToVoice speaker = new TextToVoice();
speaker.Speak("Hello World");

回答1:


Here you go . A tutorial on using the library The big downside is that it requires an SD card to store the voices.




回答2:


Using the TTS is a little bit more complicated than you expect, but it's easy to write a wrapper that gives you the API you desire.

There are a number of issues you must overcome to get it work nicely.

They are:

  1. Always set the UtteranceId (or else OnUtteranceCompleted will not be called)
  2. setting OnUtteranceCompleted listener (only after the speech system is properly initialized)

public class TextSpeakerDemo implements OnInitListener
 {
    private TextToSpeech tts;
    private Activity activity;

    private static HashMap DUMMY_PARAMS = new HashMap();
    static 
    {
        DUMMY_PARAMS.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "theUtId");
    }
    private ReentrantLock waitForInitLock = new ReentrantLock();

    public TextSpeakerDemo(Activity parentActivity)
    {
        activity = parentActivity;
        tts = new TextToSpeech(activity, this);       
        //don't do speak until initing
        waitForInitLock.lock();
    }

    public void onInit(int version)
    {        //unlock it so that speech will happen
        waitForInitLock.unlock();
    }  

    public void say(WhatToSay say)
    {
        say(say.toString());
    }

    public void say(String say)
    {
        tts.speak(say, TextToSpeech.QUEUE_FLUSH, null);
    }

    public void say(String say, OnUtteranceCompletedListener whenTextDone)
    {
        if (waitForInitLock.isLocked())
        {
            try
            {
                waitForInitLock.tryLock(180, TimeUnit.SECONDS);
            }
            catch (InterruptedException e)
            {
                Log.e("speaker", "interruped");
            }
            //unlock it here so that it is never locked again
            waitForInitLock.unlock();
        }

        int result = tts.setOnUtteranceCompletedListener(whenTextDone);
        if (result == TextToSpeech.ERROR)
        {
            Log.e("speaker", "failed to add utterance listener");
        }
        //note: here pass in the dummy params so onUtteranceCompleted gets called
        tts.speak(say, TextToSpeech.QUEUE_FLUSH, DUMMY_PARAMS);
    }

    /**
     * make sure to call this at the end
     */
    public void done()
    {
        tts.shutdown();
    }
}



回答3:


A good working example of tts usage can be found in the "Pro Android 2 book". Have a look at their source code for chapter 15.




回答4:


There are third-party text-to-speech engines. Rumor has it that Donut contains a text-to-speech engine, suggesting it will be available in future versions of Android. Beyond that, though, there is nothing built into Android for text-to-speech.




回答5:


Donut has this: see the android.speech.tts package.



来源:https://stackoverflow.com/questions/1160876/android-speech-how-can-you-read-text-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!