Android的讲话 - 你怎么能在Android的阅读的文本?(Android speech -

2019-06-26 11:12发布

你怎么能读取数据,也就是将简单的文本字符串中的Android语音(语音)?

是否有一个API,我可以做这样的事情:

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

Answer 1:

在这里你去 。 在使用该库的教程最大的缺点是,它需要一个SD卡存储的声音。



Answer 2:

使用TTS比预期稍微有点复杂,但它很容易编写一个包装,让你你想要的API。

有一些你必须克服把它很好地工作的问题。

他们是:

  1. 始终将UtteranceId(或者OnUtteranceCompleted不会被调用)
  2. 设置OnUtteranceCompleted监听器(仅后的语音系统进行适当的初始化)

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();
    }
}


Answer 3:

TTS用法的良好的工作的例子可以在“临Android的2书”中找到。 看看他们的源代码,第15章。



Answer 4:

有第三方文本到语音引擎。 有传言说,甜甜圈包含一个文本到语音转换引擎,暗示这将是Android的未来版本。 除此之外,虽然没有什么内置的Android文本到语音转换。



Answer 5:

甜甜圈有这样的:看到android.speech.tts包。



文章来源: Android speech - how can you read text in Android?