San*_*hra 12 html php speech text-to-speech google-text-to-speech
我正在尝试将单词转换为语音.
直到现在我都试过这个:
<?php
$text = "Hello this is a test for voice api of google";
// Name of the MP3 file generated using the MD5 hash
$file = md5($text);
// Save the MP3 file in this folder with the .mp3 extension
$file = "audio/" . $file .".mp3";
if($file) {
echo "created";
} else {
echo "not created";
}
// If the MP3 file exists, do not create a new request
if (!file_exists($file)) {
$mp3 = file_get_contents(
'http://translate.google.com/translate_tts?q=' . $text);
echo "hello";
file_put_contents($file, $mp3);
} else {
echo "hii";
}
?>
Run Code Online (Sandbox Code Playgroud)
在我的html文件中:
<audio controls="controls" autoplay="autoplay">
<source src="<?php echo $file; ?>" type="audio/mp3" />
</audio>
Run Code Online (Sandbox Code Playgroud)
我正在创建hello和输出中的音频播放器.但是没有文件播放,也没有在文件夹中创建?
kub*_*ube 12
您尝试访问的网址存在问题.它被打破 !你应该先尝试一下.我在FF控制台上找到的新URL是:
http://translate.google.com/translate_tts?ie=UTF-8&q=Hello&tl=en&total=1&idx=0&textlen=5&prev=input
对于单个单词Hello.而且你看到你必须在textlen中指定语言和文本的长度,即使它确实适用于我尝试的所有句子而不改变这个var.
另一个问题是你必须urlencode()你的文本,否则你将有一个带有重音符号和标点符号的错误.所以下载MP3的行变为:
// Language of the sentence
$lang = "fr";
$mp3 = file_get_contents(
'http://translate.google.com/translate_tts?ie=UTF-8&q='. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');
Run Code Online (Sandbox Code Playgroud)所以完整的代码看起来像:
<?php
$text = "Bonjour, comment allez vous ?";
// Yes French is a beautiful language.
$lang = "fr";
// MP3 filename generated using MD5 hash
// Added things to prevent bug if you want same sentence in two different languages
$file = md5($lang."?".urlencode($text));
// Save MP3 file in folder with .mp3 extension
$file = "audio/" . $file . ".mp3";
// Check folder exists, if not create it, else verify CHMOD
if (!is_dir("audio/"))
mkdir("audio/");
else
if (substr(sprintf('%o', fileperms('audio/')), -4) != "0777")
chmod("audio/", 0777);
// If MP3 file exists do not create new request
if (!file_exists($file))
{
// Download content
$mp3 = file_get_contents(
'http://translate.google.com/translate_tts?ie=UTF-8&q='. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');
file_put_contents($file, $mp3);
}
?>
Run Code Online (Sandbox Code Playgroud)
小智 5
我找到了:
https://translate.google.com.vn/translate_tts?ie=UTF-8&client=tw-ob&q=ANYTHING_TEXT&tl=YOUR_LANGUAGE_CODE
Run Code Online (Sandbox Code Playgroud)
重要的: client=tw-ob
YOUR_LANGUAGE_CODE 可以是 en,us,uk,vi 等。