Android 语音 - 如何在 Android 中阅读文本?

发布于 2024-07-28 22:32:45 字数 172 浏览 1 评论 0原文

如何在 Android 中读取数据,即将简单的文本字符串转换为语音(语音)?

有没有一个API可以让我做这样的事情:

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

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");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

愛放△進行李 2024-08-04 22:32:45

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

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();
    }
}
葮薆情 2024-08-04 22:32:45

给您使用该库的教程最大的缺点是它需要 SD 卡来存储语音。

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

萌化 2024-08-04 22:32:45

tts 使用的一个很好的工作示例可以在“Pro Android 2 book”中找到。 查看第 15 章的源代码。

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.

木緿 2024-08-04 22:32:45

有第三方文本转语音引擎。 有传言称 Donut 包含文本转语音引擎,这表明它将在未来的 Android 版本中提供。 但除此之外,Android 中并没有内置任何用于文本转语音的功能。

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.

深居我梦 2024-08-04 22:32:45

Donut 有这个:请参阅 android.speech.tts 包。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文