如何使用谷歌文本到语音API中的窗口形成的? [英] How can I use google text to speech api in windows form?

查看:173
本文介绍了如何使用谷歌文本到语音API中的窗口形成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用谷歌文本到语音在我的Windows窗体应用程序,它会读取标签。我添加System.Speech参考。它如何能够读取一个按钮单击事件标签吗?
http://translate.google.com/translate_tts?q=testing谷歌+讲话+ 这是谷歌文本到语音的API,或者我如何使用微软的原生文本到语音?

I want to use google text to speech in my windows form application, it will read a label. I added System.Speech reference. How can it read a label with a button click event? http://translate.google.com/translate_tts?q=testing+google+speech This is the google text to speech api, or how can I use microsoft's native text to speech?

推荐答案

您可以通过播放使用的这个问题的答案(我花了一段时间,但我的真正的溶液):

You can use Google's TTS API from your WinForm application by playing the response using a variation of this question's answer (it took me a while but I have a real solution):

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.FormClosing += (sender, e) =>
            {
                if (waiting)
                    stop.Set();
            };
    }

    private void ButtonClick(object sender, EventArgs e)
    {
        var clicked = sender as Button;
        var relatedLabel = this.Controls.Find(clicked.Tag.ToString(), true).FirstOrDefault() as Label;

        if (relatedLabel == null)
            return;

        var playThread = new Thread(() => PlayMp3FromUrl("http://translate.google.com/translate_tts?q=" + HttpUtility.UrlEncode(relatedLabel.Text)));
        playThread.IsBackground = true;
        playThread.Start();
    }

    bool waiting = false;
    AutoResetEvent stop = new AutoResetEvent(false);
    public void PlayMp3FromUrl(string url)
    {
        using (Stream ms = new MemoryStream())
        {
            using (Stream stream = WebRequest.Create(url)
                .GetResponse().GetResponseStream())
            {
                byte[] buffer = new byte[32768];
                int read;
                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
            }

            ms.Position = 0;
            using (WaveStream blockAlignedStream =
                new BlockAlignReductionStream(
                    WaveFormatConversionStream.CreatePcmStream(
                        new Mp3FileReader(ms))))
            {
                using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
                {
                    waveOut.Init(blockAlignedStream);
                    waveOut.PlaybackStopped += (sender, e) =>
                    {
                        waveOut.Stop();
                    };

                    waveOut.Play();
                    waiting = true;
                    stop.WaitOne(10000);
                    waiting = false;
                }
            }
        }
    }
}

注意:上面的代码需要n音讯工作(自由/开源)和使用系统报表名.web 的System.Threading NAudio.Wave

NOTE: The above code requires NAudio to work (free/open source) and using statements for System.Web, System.Threading, and NAudio.Wave.

我的 Form1中上有2控件:


  1. 命名的标签卷标1

  2. 命名的按钮按钮1 标签 卷标1 的(用于按钮绑定到其标签)

  1. A Label named label1
  2. A Button named button1 with a Tag of label1 (used to bind the button to its label)

上面的代码可以略微如果你有使用类似(未经测试)每个按钮/标签组合不同的事件简化

The above code can be simplified slightly if a you have different events for each button/label combination using something like (untested):

    private void ButtonClick(object sender, EventArgs e)
    {
        var clicked = sender as Button;

        var playThread = new Thread(() => PlayMp3FromUrl("http://translate.google.com/translate_tts?q=" + HttpUtility.UrlEncode(label1.Text)));
        playThread.IsBackground = true;
        playThread.Start();
    }

有这种解决方案虽然问题(这个名单可能是不完整的;我敢肯定的意见和现实世界的用法会发现其他人):

There are problems with this solution though (this list is probably not complete; I'm sure comments and real world usage will find others):


  1. 注意 stop.WaitOne(10000); 中的第一个代码片段。 10000表示要发挥最大的声音为10秒,因此需要,如果你的标签需要长于读给进行调整。这是必要的,因为n音讯(v1.5.4.0)的当前版本似乎有一个问题,确定何时流进行播放。它可固定在以后的版本或者有,我没有花时间去找到一个解决办法。一个临时的解决办法是使用 ParameterizedThreadStart ,将采取超时作为参数传递给线程。这将允许可变超时,但不会在技术上解决该问题。

  2. 更重要的是,谷歌TTS API是非官方的(即不被非谷歌应用程序使用),它可能在任何时候,恕不另行通知更改。如果你需要的东西,将在商业环境中工作,我建议要么MS TTS解决方案(如你的问题建议)或许多商业的替代品之一。其中无往往是连这个简单不过。

  1. Notice the stop.WaitOne(10000); in the first code snippet. The 10000 represents a maximum of 10 seconds of audio to be played so it will need to be tweaked if your label takes longer than that to read. This is necessary because the current version of NAudio (v1.5.4.0) seems to have a problem determining when the stream is done playing. It may be fixed in a later version or perhaps there is a workaround that I didn't take the time to find. One temporary workaround is to use a ParameterizedThreadStart that would take the timeout as a parameter to the thread. This would allow variable timeouts but would not technically fix the problem.
  2. More importantly, the Google TTS API is unofficial (meaning not to be consumed by non-Google applications) it is subject to change without notification at any time. If you need something that will work in a commercial environment I'd suggest either the MS TTS solution (as your question suggests) or one of the many commercial alternatives. None of which tend to be even this simple though.

要回答的另一面你的问题:


To answer the other side of your question:

System.Speech.Synthesis.SpeechSynthesizer 类的<​​em>多的更容易使用,你可以在它是提供可靠的(如与谷歌的API,它可以明天去)计数。

The System.Speech.Synthesis.SpeechSynthesizer class is much easier to use and you can count on it being available reliably (where with the Google API, it could be gone tomorrow).

这实在是为包括对 System.Speech 引用的参考,并为方便:

It is really as easy as including a reference to the System.Speech reference and:

public void SaySomething(string somethingToSay)
{
    var synth = new System.Speech.Synthesis.SpeechSynthesizer();

    synth.SpeakAsync(somethingToSay);
}

这的只是工作的。

尝试使用谷歌API TTS是一个有趣的实验,但我会很难表明它在生产中使用,如果你不想支付一个商业的选择,微软的解决方案是因为它得到一样好。

Trying to use the Google TTS API was a fun experiment but I'd be hard pressed to suggest it for production use, and if you don't want to pay for a commercial alternative, Microsoft's solution is about as good as it gets.

这篇关于如何使用谷歌文本到语音API中的窗口形成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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