微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 Amazon Polly 自动播放 MP3

如何解决如何使用 Amazon Polly 自动播放 MP3

我正在为文本转语音实施 Amazon Polly。我能够成功生成一个 mp3 文件,但我希望该 mp3 播放能够自动播放。请告诉我应该在代码添加什么来添加功能。所以,当我点击按钮时,它应该开始说出文本框中的内容

这是我的代码

<?PHP
require 'vendor/autoload.PHP';

use Aws\Polly\PollyClient;

if ( isset($_POST['file']) ) {

    try {
        $config = [
            'version' => 'latest','region' => 'us-west-1','credentials' => [
                'key' => '***','secret' => '***',]
            ];

        $client = new PollyClient($config);

        $val = $_POST['file'];

        $args = [
            'OutputFormat' => 'mp3','Text' => $val,//'TextType' => 'text','VoiceId' => 'Matthew',//pass preferred voice id here
        ];

        $result = $client->synthesizeSpeech($args);

        $resultData = $result->get('AudioStream');//->getContents();

        /*header('Content-length: ' . strlen($resultData));
        header('Content-disposition: attachment; filename="text-to-speech.mp3"');
        header('X-Pad: avoid browser bug');
        header('Cache-Control: no-cache');*/

        $myfile = fopen("test.mp3","w");
        fwrite($myfile,$resultData);
        fclose($myfile);

        echo 'test.mp3';//$resultData;

    } catch(Exception $e) {
        echo $e->getMessage();
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <Meta charset="UTF-8">
  <title>Document</title>

</head>
<body>

  <audio>
    <source class="track" src="" type="audio/mpeg">
  </audio>

  <form method="post" enctype="multipart/form-data">
      <input type="text" name="file" />
      <button type="submit" name="submit">Submit</button>
  </form>


</body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。