作为延续,由于 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;
?>