【问题标题】:Text2Speech Error, How can I play audio by entering URL into browser directly?Text2Speech 错误,如何通过直接在浏览器中输入 URL 来播放音频?
【发布时间】:2019-04-13 00:19:52
【问题描述】:

我让 text2speech 工作,但我想使用 GET 方法,我该怎么做? 我的意思是我想通过这样的 URL 转换 text2speech: http://localhost/txt2speech/v.php?textbox=Hello Word

如果我在浏览器中输入该 URL,我希望它能够播放音频。

我可以成功地将文本转换为语音,但问题是它播放的是同一个文件。

示例

如果我发送http://localhost/txt2speech/v.php?textbox=HelloWord 然后如果我发送http://localhost/txt2speech/v.php?textbox=SecondString

它播放 HelloWorld,但我希望它播放 SecondString 但是,如果我在不同的窗口中尝试,它将播放 SecondString 文件。

我还有一个问题。如果我传递一个带有空格的字符串,它将不起作用。

示例

http://localhost/txt2speech/v.php?textbox=Hello This is sentence with spaces

这些是我正在使用的文件:

index.php

<html>
<body> 
<h2>Text to Speech PHP Script</h2>

<form action="v.php" method="GET">
Enter your text: <input name="textbox"></input>
</form>

</body>
</html>

v.php

<?php
 if($_GET){
     $text = substr($_GET['textbox'], 0, 100);
   $file  = 'filename';
 $file = "audio/" . $file . ".mp3";
  $mp3 = file_get_contents("https://translate.google.com.vn/translate_tts?ie=UTF-8&q=$sname+&tl=en&client=tw-ob");
 file_put_contents($file, $mp3);
}
?>

<?php  if($_GET){?>
<audio controls="controls" autoplay="autoplay">
  <source src="<?php echo $file; ?>" type="audio/mp3" />
</audio>
<?php }?>

【问题讨论】:

    标签: php html text text-to-speech speech


    【解决方案1】:

    如果您的邮政编码有效,您可以将帖子更改为如下所示:

    index.php

    <html>
    <body> 
    <h2>Text to Speech PHP Script</h2>
    
    <form action="v.php" method="get">
    Enter your text: <input name="textbox"></input>
    </form>
    
    
    <?php  if($_GET){?>
    <audio controls="controls" autoplay="autoplay">
      <source src="<?php echo $file; ?>" type="audio/mp3" />
    </audio>
    <?php }?>
    </body>
    </html>
    

    v.php

    <?php
     if($_GET){
         $text = substr($_GET['textbox'], 0, 100);
       $file  = 'filename';
     $file = "audio/" . $file . ".mp3";
     # Convert input to proper encoded url
     $sname = urlencode ( $sname );
      $mp3 = file_get_contents("https://translate.google.com.vn/translate_tts?ie=UTF-8&q=$sname+&tl=en&client=tw-ob");
     file_put_contents($file, $mp3);
    }
    ?>
    

    为防止空格破坏您的网址,请在 php 中使用 urlencode 函数。

    【讨论】:

    • 我知道get和post的区别,我已经试过了,所以我在这里发帖
    • 好的,它对我有用,我还是在 v.php 中添加了音频控制 感谢您的快速响应
    • 但问题是当我发送带空格的字符串时它返回错误,
    • 你得到的价值是多少? %20?您可以在解析之前使用函数来清理输入:stackoverflow.com/a/2109339/3163075
    • 我什么也没说错误:警告:file_get_contents(translate.google.com.vn/translate_tts?ie=UTF-8&q=hello fgfg+&tl=en&client=tw-ob):打开流失败:HTTP 请求失败! HTTP/1.0 400 Bad Request in C:\xampp\htdocs\txt2speech\v.php 第 16 行 和行号。 16 是:$mp3 = file_get_contents("translate.google.com.vn/…);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2012-11-10
    • 2011-03-05
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    相关资源
    最近更新 更多