4

在您的帮助下,我现在有了一个 ajax 函数,它可以立即对输入值做出反应。(使用 AJAX 功能更改提交按钮以立即对输入做出反应

此功能以俄语显示输入数字的单词。现在,我想在单词的左侧添加一个播放图标,单击它即可发音该单词。

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

另一个问题: 1. 此函数最多允许 100 个字符发音,因此脚本应将大输入(>100 个字符)拆分为多个连续请求,例如最大可能数字 999999999999999。

4

1 回答 1

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;

?>
于 2014-06-22T02:24:24.043 回答