title: AWS - Amazon Polly Text To Speech
body: <p>I have doubts about the <strong>"text-to-speech"</strong> <strong>Amazon Polly</strong> service. <br>
  I've integrated this service in my chatbot, in order to describe vocally what the bot writes to the user in chat. <br>
  It works pretty well, but I don't know if it is possible to stop the voice early, before <em>she</em> (I chose a female voice) finishes speaking. Sometimes I need to go further in the conversation and I don't want to listen until the end of the sentence.</p>
  
  <p>This is the code used for the integration: <br></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>//Html side
  function textToSpeech(text) {
    $.ajax({
      type: 'GET',
      url: '/Chat/TextToSpeech?text=' + text,
      
      cache: false,
      success: function (result) {
      
        var audio = document.getElementById('botvoice');
        $("#botvoice").attr("src", "/Audios/" + result);
        audio.load();                 
        audio.play();
      }
    });
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Controller side:</p>
  
  <pre><code>public ActionResult TextToSpeech(string text)
  {
      string filename = "";
      try
      {
          AWSCredentials credentials = new StoredProfileAWSCredentials("my_credential");
          AmazonPollyClient client = new AmazonPollyClient(credentials, Amazon.RegionEndpoint.EUWest1);
  
          // Create describe voices request.
          DescribeVoicesRequest describeVoicesRequest = new DescribeVoicesRequest();
          // Synchronously ask Amazon Polly to describe available TTS voices.
          DescribeVoicesResponse describeVoicesResult = client.DescribeVoices(describeVoicesRequest);
          List&lt;Voice&gt; voices = describeVoicesResult.Voices;
  
  
          // Create speech synthesis request.
          SynthesizeSpeechRequest synthesizeSpeechPresignRequest = new SynthesizeSpeechRequest();
          // Text
          synthesizeSpeechPresignRequest.Text = text;
          // Select voice for synthesis.
          synthesizeSpeechPresignRequest.VoiceId = voices[18].Id;
          // Set format to MP3.
          synthesizeSpeechPresignRequest.OutputFormat = OutputFormat.Mp3;
          // Get the presigned URL for synthesized speech audio stream.
  
          string current_dir = AppDomain.CurrentDomain.BaseDirectory;
          filename = CalculateMD5Hash(text) + ".mp3";
          var path_audio = current_dir + @"\Audios\" + filename;
  
          var presignedSynthesizeSpeechUrl = client.SynthesizeSpeechAsync(synthesizeSpeechPresignRequest).GetAwaiter().GetResult();
  
          FileStream wFile = new FileStream(path_audio, FileMode.Create);
          presignedSynthesizeSpeechUrl.AudioStream.CopyTo(wFile);
          wFile.Close();
      }
      catch (Exception ex)
      {
          filename = ex.ToString();
      }
  
      return Json(filename, JsonRequestBehavior.AllowGet);
  }
  </code></pre>
  
  <p>An input text is present in my chat (obviously) for writing and sending (by pressing ENTER on the keyboard) the question to the bot. I tried to put the command <code>audio.src=""</code> in the handler, and she stops to talk but the chat still remains blocked... It seems like it waits the end of the audio stream. I have to refresh the page to see new messages and responses. <br></p>
  
  <p>Is there any Amazon function that I can call with a particular parameter set, in order to notify the service that I want to stop and clear the audio stream?</p>