0

我仍然遇到此代码的问题。我需要将参数设置为函数,以便将其转换为 .mp3 文件。使用这一行:$tts->setText($row['raspuns']);不会发生任何事情,但如果我编写$tts->setText("Hello World!");它可以完美运行,这使我得出结论,我必须找到正确的代码才能使 tts 获取文本。任何人都可以帮助我吗?

<html>
    <head>
        <title>
            Bot
        </title>
        <link type="text/css" rel="stylesheet" href="main.css" />
    </head>
    <body>
        <form action="bot.php "method="post">
            <lable>You:<input type="text" name="intrebare"></lable>
            <input type="submit" name="introdu" value="Send">
        </form>
    </body>
</html>
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("robo") or die(mysql_error());

$intrebare=$_POST['intrebare'];
$query = "SELECT * FROM dialog where intrebare = '$intrebare'"; 
$result = mysql_query($query) or die(mysql_error());
$row = $result;
?>

<?php
require "tts.php";
$tts = new TextToSpeech();
$tts->setText($row['raspuns']);
//$tts->setText("Hello World!");
$tts->saveToFile("voice.mp3");
$file='voice.mp3';
?>

<div id="history">
<?php       

    while (true == ($row = mysql_fetch_array($result))) {
    echo "<b>The robot says: </b><br />";
    echo $row['raspuns'];
    echo "<embed src =\"$file\" hidden=\"true\" autostart=\"true\"></embed>";
}
?>
</div>

这是 tts.php 文件:

<?php
class TextToSpeech {
    public $mp3data;
    function __construct($text="") {
        $text = trim($text);
        if(!empty($text)) {
            $text = urlencode($text);
            $this->mp3data = file_get_contents("http://translate.google.com/translate_tts?tl=en&q={$text}");
        }
    }

    function setText($text) {
        $text = trim($text);
        if(!empty($text)) {
            $text = urlencode($text);
            $this->mp3data = file_get_contents("http://translate.google.com/translate_tts?tl=en&q={$text}");
            return  $this->mp3data;
        } else { return false; }
    }

    function saveToFile($filename) {
        $filename = trim($filename);
        if(!empty($filename)) {
            return file_put_contents($filename,$this->mp3data);
        } else { return false; }
    }
}
?>
4

1 回答 1

1

根据 mysql_query 的文档,

返回的结果资源应该传递给 mysql_fetch_array() 和其他处理结果表的函数,以访问返回的数据。

所以而不是

$row = $result;

你应该有

$row = mysql_fetch_array($result);
于 2012-12-09T12:05:23.540 回答