php – 为适用于多种浏览器的脚本添加Google TTS
发布时间:2020-12-13 22:52:53 所属栏目:PHP教程 来源:网络整理
导读:在你的帮助下,我现在有了一个ajax函数,它可以立即对输入值做出反应. ( Change the Submit-Button with AJAX-function for instantly reacting for an input) 此功能以俄语显示输入数字的单词. 现在我想在单词中添加一个播放图标左侧,单击它即可发出单词. 我
|
在你的帮助下,我现在有了一个ajax函数,它可以立即对输入值做出反应.
( Change the Submit-Button with AJAX-function for instantly reacting for an input) 此功能以俄语显示输入数字的单词. 我找到了Google TTS(文字转语音)的解决方案,但在我的示例中,它仅适用于Google Chrome. IE和Firefox(最新版本)不起作用. 另一个问题: 解决方法
作为延续,因为ajax(正常进程)现在正在工作,就像在测试站点上一样实现它.考虑这个例子:
<form method="POST" action="index.php">
<label for="zahl">Zahl:</label> <br/>
<input id="zahl" name="zahl" type="number" size="15" maxlength="15"><br/><br/>
<img src="http://lern-online.net/bilder/symbole/play.png" id="playsound" style="display: none; " />
<span id="results" style="width: 400px;"></span> <!-- results appear here -->
</form>
<div class="player"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<!-- <script src="jquery.min.js"></script> -->
<script type="text/javascript">
$(document).ready(function(){
$('#zahl').on('keyup',function(){
var input = $(this).val();
if(input != '') {
// ajax request server
$.ajax({ url: 'index.php',type: 'POST',dataType: 'text',data: {value: input},success: function(response) {
$('#results').text(response); // append to result div
$('#playsound').show();
}
});
} else {
$('#results').text('');
$('#playsound').hide();
}
});
// add this,since it spawns new embed tags every click
$('#playsound').click(function(){
var input = encodeURIComponent($('#results').text());
$('.player').html('<audio controls="" autoplay="" name="media" hidden="true" autostart><source src="sound.php?text='+input+'" type="audio/mpeg"></audio>');
});
});
</script>
创建一个处理声音请求的新php文件: 叫它sound.php <?php
$txt = $_GET['text'];
$txt = urlencode($txt);
header("Content-type: audio/mpeg");
$url ="http://translate.google.com/translate_tts?q=$txt&tl=ru&ie=UTF-8";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_TIMEOUT,30);
$audio = curl_exec($ch);
echo $audio;
?>
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
