2014-06-22 64 views
4

我现在有一个ajax函数,它立即对输入值做出反应。 (Change the Submit-Button with AJAX-function for instantly reacting for an input在几个浏览器中添加Google TTS脚本

此功能以俄语显示输入数字的单词。 现在我想从单词添加左边的一个播放图标,通过单击它来发出单词。

我找到了Google TTS(文本到语音转换)的解决方案,但在我的示例中,它仅适用于Google Chrome。 IE和Firefox(最新版本)不工作。

另一个问题: 1.此功能允许最大100个字符到pronunciate,所以脚本应该将裂变大输入(> 100个字符)转换成多个连续的请求exampel为最大可能的数目999999999999999.

回答

1

作为因为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; 

?> 
+0

的方式,并没有对IE浏览器,因为这个测试还即时通讯使用Linux – user1978142

+0

谢谢!它只适用于谷歌浏览器,并有大量的问题。我已经添加了这个问题。为什么它现在使用jQuery 1.11.1? – Grischa

+0

@Grischa其在评论里面的答案,有一个链接,你可能想检查一下,它详细说明发生了什么 – user1978142