Esempio n. 1
0
        public static async Task <string> Pronounce(string word, string language)
        {
            try
            {
                string apiKey     = ConfigurationHelper.GetAppSetting("YandexSpeechApiKey");
                string requestUrl = String.Format("generate?key={0}&format=mp3&speaker=jane&lang={1}&text={2}",
                                                  apiKey, language, WebUtility.UrlEncode(word));

                string fileName = Path.Combine(Path.GetTempPath(), requestUrl.GenerateHash()) + ".mp3";
                if (File.Exists(fileName))
                {
                    return(fileName);
                }

                using (FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (Stream stream = await SpeechClient.GetStreamAsync(requestUrl).ConfigureAwait(false))
                    {
                        stream.CopyTo(fileStream);
                        return(fileStream.Name);
                    }
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }