如何使用 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 (将#修改为@)