我有一个 HTML5 元素,可以在点击时播放嵌入的音频文件。这是代码的样子:
HTML:
<span class="sound">
<audio id="yourAudio" preload="none" onplay="playing(this);" onended="stopped(this);">
<source src="/sandboxassets/pronunciations/estest106f444b36e7b5fdb88a10706d397dc2.mp3" type="audio/mpeg">
</audio>
<a href="javascript:;" id="audioControl" onclick="playclip(this);" title="Hear it spoken">
<i class="fa fa-2x fa-volume-down pronounce"></i>
</a>
</span>
JS:
// play pronunciation file
function playclip(mp3){
mp3.parentNode.getElementsByTagName('audio')[0].play();
return false; // Prevent Default Action
}
function playing(thisicon){
// var icon = document.getElementsByClassName("pronounce")[0];
var icon = thisicon.parentNode.getElementsByClassName('pronounce')[0];
icon.className = "fa fa-2x fa-volume-up pronounce";
}
function stopped(thisicon){
// var icon = document.getElementsByClassName("pronounce")[0];
var icon = thisicon.parentNode.getElementsByClassName('pronounce')[0];
icon.className = "fa fa-2x fa-volume-down pronounce";
}
此设置至少在桌面浏览器中运行良好:Chrome、Edge 和 Opera(尚未在 Safari、IE 和 Firefox 上进行测试)。但是,点击在 Chrome Android 上没有任何作用。知道我可能在哪里出错吗?该代码位于 www.peppyburro.com/sandboxdictionary/fugar。
这里有一个标题完全相同的问题,但提供的解决方案似乎不是我要找的,因为我没有在我的网站上明确添加 .htaccess 密码保护。
最佳答案
Android 和 iPhone 触摸事件
touchstart ↔ mousedown
touchend ↔ mouseup
touchmove ↔ mousemove
touchcancel
document.getElementById('yourAudio').addEventListener("mouseup", tapOrClick, false);
document.getElementById('yourAudio').addEventListener("touchend", tapOrClick, false);
function tapOrClick(e) {
var mp3 = e.target;
mp3.parentNode.getElementsByTagName('audio')[0].play();
}
关于javascript - HTML5 音频无法在 Mobile Chrome (Android) 上播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41489921/