Home > database >  Text to audio give error using php laravel
Text to audio give error using php laravel

Time:10-12

When i give $les small string like "hello how are you" it works fine but when string length is high like 500words then it gives error! if i give

$les = 'Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, hello hihoe hihh';

then it works fine if i enter just one letter more then it gives error

Thanks for any solution enter image description here

$les  = $lesson->full_text;

    /* for text to audio */
    
    $txt  =htmlspecialchars($les);
    $txt=rawurlencode($txt);
    
    $audio = file_get_contents('https://translate.google.com/translate_tts?ie=UTF-8&client=gtx&q='.$txt.'&tl=es');
    $speech="<audio controls='controls'><source src='data:audio/mpeg;base64,".base64_encode($audio)."'></audio>";
    echo $speech; exit();
    

    /* end text to audio */

CodePudding user response:

This is because of character length limitation.

Hope this trick helps:

public function index() {
    $max = 80;
    $les = 'test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test';

    if (strlen($les) < $max) {
        $txt[] = rawurlencode(htmlspecialchars($les));
    } else {
        $txt = chunk_split($les, $max);
        $txt = explode("\r\n", $txt);
    }

    foreach ($txt as $t) {
        if (strlen($t) == 0) { continue; }
        $t = rawurlencode(htmlspecialchars(trim($t)));
        $audio = file_get_contents('https://translate.google.com/translate_tts?client=gtx&q='.$t.'&tl=en-US');
        echo "<audio controls='controls'><source src='data:audio/mpeg;base64,".base64_encode($audio)."'></audio>";
    }
}

After this you could do your goal by joining audios or play each other in sequence.

  • Related