如何在移动设备上播放音频? [英] How to make audio playback on work on mobile?
问题描述
在桌面浏览器上无法播放音频,但是在移动设备上,它无法播放任何音频.我在控制台上也没有收到任何错误.
On desktop browsers im able to playback audio however, on mobile it doesnt playback any audio. Im not getting any errors in the console either.
如何在移动设备上播放音频?
How can i make audio playback work on mobile?
function initAudio(){
var audio, dir, ext, mynoteslist;
dir = "audio/";
ext = ".mp3";
// Audio Object
audio = new Audio();
audio.src = dir+ext;
var arr = document.getElementsByClassName("mynoteslist");
for (var k=0; k<arr.length; k++){
arr[k].addEventListener("change", changeNote);
}
// Functions
function changeNote(event){
audio.src = dir+event.target.value+ext;
audio.play();
}
}
window.addEventListener("load", initAudio);
推荐答案
除非由于用户手势的直接结果,否则许多移动设备将不会播放音频(或视频,如果具有配乐且未静音).
Many mobile devices will not play audio (or video if it has a soundtrack and is not muted) unless as a direct result of a user gesture.
例如对于Apple移动设备
For example for an Apple mobile device
例如,对video.play()的调用必须直接来自处理触摸,点击,双击或按下按键事件的处理程序
the call to video.play(), for example, must have directly resulted from a handler for a touchend, click, doubleclick, or keydown event
因此,您可能必须使用户单击或触摸或感测按键按下,然后使用该事件来播放音频.似乎更改事件还不够.
You will probably therefore have to get the user to click or touch or sense a key down and use that event to play the audio. It seems that a change event will not be enough.
这篇关于如何在移动设备上播放音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!