Text2Speech错误,如何通过直接在浏览器中输入URL来播放音频?

问题描述 投票:-1回答:1

我有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个回答
0
投票

如果您的帖子代码有效,您可以更改帖子,如下所示:

的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函数。

© www.soinside.com 2019 - 2024. All rights reserved.