在 touchstart 上播放音频
Playing audio on touchstart
我正在尝试在 JQuery 移动设备上启动触摸启动功能时播放声音。我该怎么做呢?下面的代码不会在移动浏览器中播放音频元素,但在桌面浏览器上运行良好。非常感谢!
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'DING.mp3');
$("#LNum").bind("touchstart", function() {
audioElement.play();
}
您正在尝试在 <audio>
标签中添加 src 属性而不是 <source>
。
<audio controls>
<source src="ding.ogg" type="audio/ogg">
<source src="ding.mp3" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>
您需要在 audioElement
中创建一个 source
元素。
然后将 src
属性应用于源标记。
我使用以下解决方案
HTML
<audio class="my_audio" controls preload="none" style="display: none" id="audioControl">
<source class="notificationTone" type="audio/mpeg">
</audio>
JS
$("#LNum").bind("touchstart click tap", function() {
$(".notificationTone").attr('src', "somemp3.mp3");
$(".my_audio").trigger('load');
$(".my_audio").trigger('play');
}
我正在尝试在 JQuery 移动设备上启动触摸启动功能时播放声音。我该怎么做呢?下面的代码不会在移动浏览器中播放音频元素,但在桌面浏览器上运行良好。非常感谢!
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'DING.mp3');
$("#LNum").bind("touchstart", function() {
audioElement.play();
}
您正在尝试在 <audio>
标签中添加 src 属性而不是 <source>
。
<audio controls>
<source src="ding.ogg" type="audio/ogg">
<source src="ding.mp3" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>
您需要在 audioElement
中创建一个 source
元素。
然后将 src
属性应用于源标记。
我使用以下解决方案
HTML
<audio class="my_audio" controls preload="none" style="display: none" id="audioControl">
<source class="notificationTone" type="audio/mpeg">
</audio>
JS
$("#LNum").bind("touchstart click tap", function() {
$(".notificationTone").attr('src', "somemp3.mp3");
$(".my_audio").trigger('load');
$(".my_audio").trigger('play');
}