3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS SDK for JavascriptからPollyを呼び出してしゃべらせる

Posted at

背景

AWSSDKforJavascriptのPollyサンプルに、入力した文字列をそのまましゃべらせる方法が書いていなかったのでメモ。

<!DOCTYPE html>
<html>
<head>
  <title>AWS SDK for JavaScript in the Browser</title>
  <script src="aws-sdk.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script>
  //クライアントにキーが丸見えになるので非推奨
  AWS.config.update({accessKeyId: '###', secretAccessKey: '###', region: 'us-west-2'});
  //Pollyテスト
  /* This operation detects faces in an image stored in an AWS S3 bucket. */
  var params = {
    OutputFormat: "mp3",
    SampleRate: "8000",
    Text: "Amazonから最新情報です。Kindleの電子書籍読み上げサービス「Kindle Speach」がリリースしました。無料のコンテンツをお試しできますが、いかがでしょうか?",
    TextType: "text",
    VoiceId: "Mizuki"
  };
  var polly = new AWS.Polly();
  polly.synthesizeSpeech(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
    var elementId = "audioElement" + new Date().valueOf().toString();
    var audioElement = document.createElement('audio');
    audioElement.setAttribute("id", elementId);
    document.body.appendChild(audioElement);
    var uInt8Array = new Uint8Array(data.AudioStream);
    var arrayBuffer = uInt8Array.buffer;
    var blob = new Blob([arrayBuffer]);
    var url = URL.createObjectURL(blob);
    audioElement.src = url;
    audioElement.play();
  });
  </script>


</head>
<body>
  <div id="keys"/>
</body>
</html>
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?