我需要在移动浏览器中使用Google翻译服务,以使用户可以通过单击按钮或超链接来收听移动Web应用程序中的选定文本。
以下代码可在台式机浏览器(Google Chrome 75; Edge)中使用,但不适用于移动浏览器iOS 12.3.1和Safari或Chrome移动版75.0.3770.103。
<script>
var playBtn = document.getElementById('play');
var stopBtn = document.getElementById('stop');
var playSound = function() {
audio.play();
};
playBtn.addEventListener('click', playSound, false);
stopBtn.addEventListener('click', function(){audio.pause()}, false);
</script>
<body>
<h1>Audio Test</h1>
<p>Simple example</p>
<div>
<audio id="audio" src="http://translate.google.com/translate_tts?tl=en&q=hello%20world!&client=tw-ob" ></audio>
<button id="play">Play</button>
<button id="stop">Stop</button>
</div>
</body>
我发现问题出在Google翻译网址中。当我将音频src标准mp3 / mp4文件放入音频时,everythink都可以正常工作,但是此google translation的网址仅适用于台式机浏览器: http://translate.google.com/translate_tts?tl=en&q=hello%20world!&client=tw-ob