コード例 #1
0
        /// <summary>Speaks the specified text.</summary>
        /// <param name="text">The text.</param>
        public void Speak(string text)
        {
            try
            {
                Trace.WriteLine(string.Format("Speak: {0}", text));
                var uri  = "http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=" + cultureInfo.Name + "&q=" + Uri.EscapeUriString(text);
                var hash = Base32.Safe.Encode(uri.GetHashCode() * (long)text.Length);

                lock (this)
                {
                    if (TryPlay(Path.Combine(FilePath, hash + ".snd")))
                    {
                        return;
                    }

                    Trace.WriteLine(string.Format("Request new google translation for {0}", text));
                    byte[] data;
                    {
                        var fileName = Path.Combine(FilePath, hash + ".mp3");
                        if (File.Exists(fileName))
                        {
                            data = File.ReadAllBytes(fileName);
                        }
                        else
                        {
                            data = HttpConnection.Get(uri);
                            File.WriteAllBytes(fileName, data);
                        }
                    }
                    DecodeAndPlay(data, Path.Combine(FilePath, hash + ".snd"), text);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Could not speak desired text.", ex);
            }
        }