简体   繁体   English

如何确保Google TTS的传入语音已完全加载?

[英]How to make sure the incoming voice from Google tts have fully loaded?

Testing in low network speed, for a proper use of Google tts API from JavaScript, the case is that sometimes it reads and some times it's not, it may read very short text like 'hi', how to make sure the requested audio have load. 在低网络速度下进行测试,以正确使用JavaScript中的Google tts API,这种情况是有时读取但有时却无法读取,它可能会读取“ hi”之类的很短的文本,如何确保请求的音频已加载。

    <!DOCTYPE html>
    <html>
     <head>
       <meta charset="UTF-8">
       <title></title>
       <script>
        function playit() {
                var audio = new Audio();
                audio.src = 'http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=Okay i can read but not alwayes';
                //function(){testbeforplaying;}
                audio.play();
        }
      </script>
     </head>
     <body>
       <input type="button" value="say" onclick="playit()"/>
     </body>
    </html>

Loading audio (or video, or images) is an asynchronous operation so you need to listen to tap into the browser's callback mechanism to know when the file is ready for use. 加载音频(或视频,图像)是异步操作,因此您需要收听浏览器的回调机制,以了解何时可以使用该文件。

Update 更新

Added type to the mix, and demo: type添加到混音中,并进行演示:

Fiddle 小提琴

In case of audio (and video) you can listen to the canplay event: 如果是音频(和视频),则可以收听canplay事件:

function playit() {
    var audio = new Audio();
    audio.addEventListener('canplay', function() {
        this.play();
    }, false);
    audio.type = 'audio/mpeg';
    audio.src = '...';
}

You could also use canplaythrough instead of canplay . 您也可以使用canplaythrough代替canplay

Optionally do this: (可选)执行以下操作:

function playit() {
    var audio = new Audio();
    audio.preload = 'auto';
    audio.autoplay = true;
    audio.type = 'audio/mpeg';
    audio.src = '...';
}

There seem to be an issue with Chrome though - it cancels flat out: Chrome似乎有问题 -完全取消了:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保图像已完全加载到DOM中 - How to make sure that an image is fully loaded in the DOM 谷歌TTS的jQuery没有声音 - jquery for google tts no voice 使用postMessage API时如何确保弹出窗口已完全加载? - how to make sure that a popup window is fully loaded when using postMessage API? 如何检查Google地图是否已满载? - How to check if Google Maps is fully loaded? 我在 Java 脚本位中有来自谷歌表的数据如何进行搜索 function 并确保 50000 加载行 - I Have data from google sheet in Java script bit how to make search function and make sure the 50000 loads rows 如何在为 Google Chrome 扩展程序完全加载页面之前运行内容脚本 - How to make a content script run before a page is fully loaded for Google Chrome Extensions 如何确保在NodeJS中加载了模块 - How to make sure a module is loaded in NodeJS 页面完全加载时,确保JavaScript正在运行的最佳方法是什么? - what is the best way to make sure javascript is running when page is fully loaded? 如何确保Ajax使用.load完全加载内容 - How to make sure ajax is fully loading content using .load Ember.js:完全加载后,如何从视图中的装置访问记录? - Ember.js: How can I access records from a fixture in a view after they have been fully loaded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM