HTML5 音频无法在移动设备上播放 Chrome (Android)
HTML5 audio not playing on Mobile Chrome (Android)
我有一个 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();
}
我有一个 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();
}