0

I'm still having problems with this code. I neet to set the argument to a function in order to convert it to a .mp3 file. Using this line : $tts->setText($row['raspuns']); doesn't happen anything but if i write $tts->setText("Hello World!"); it works perfectly, which takes me to the conclusion that i have to find a correct code to make that tts get the text. Can anyone help me please?

<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>

Here's the tts.php file:

<?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; }
    }
}
?>
2
  • What is the content of your $row? are you sure you have valid text? And BTW, mysql_query is deprecated, use PDO or mysqli Commented Dec 9, 2012 at 11:54
  • 2
    $row is a row from database. I think it's valid because it works, it shows me what I need to see Commented Dec 9, 2012 at 11:57

1 Answer 1

1

According to doc of mysql_query,

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

so instead of

$row = $result;

You should have

$row = mysql_fetch_array($result);
Sign up to request clarification or add additional context in comments.

5 Comments

I've already done that but nothing happens, no data is returned
You do it in your history section, but not at the top on the tts part.
( ! ) SCREAM: Error suppression ignored for
( ! ) Warning: trim() expects parameter 1 to be string, array given in C:\wamp\www\robo\tts.php on line 13
@DanielPatilea What is your DB schema for the dialog table? and can you make a print_r of the row?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.