在touchstart上播放音频

时间:2016-06-07 20:16:56

标签: jquery jquery-mobile

我正试图在JQuery mobile上启动touchstart功能时播放声音。我该怎么做呢?下面的代码不会在移动浏览器中播放音频元素,但在桌面浏览器上可以正常工作。万分感谢!

var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'DING.mp3');

$("#LNum").bind("touchstart", function() {
     audioElement.play();
}

1 个答案:

答案 0 :(得分:0)

您正尝试在 // delete an item from the linked list public void delete(int pos) { if (countitem() > 0){ //make sure the list is not empty. ListNode<T> temp,del; if (pos== 1){ //delete the first item if(countitem()==1){ //The list contains only one item pfirst=null; plast=null; }else { //The list contains more than one item temp=pfirst; pfirst=pfirst.next; temp=null; } System.out.println("Deleted"); } else if (pos > 1 && pos <=countitem()){ //delete middle item temp=pfirst; int i; for(i=1;i<pos-1;i=i+1) {temp=temp.next;} //move to the item staying before the target item to be deleted del=temp.next; //target item to be deleted temp.next=del.next; if(del.next==null) plast=temp; //delete last item del=null; System.out.println("Deleted"); } else System.out.println("Invalid position!"); } else System.out.println("No item found"); } 代码中添加src属性,而不是<audio>

<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> 内创建source元素。 然后将audioElement属性应用于源标记。

我使用以下解决方案

<强> HTML

src

<强> JS

 <audio class="my_audio" controls preload="none" style="display: none" id="audioControl">
    <source class="notificationTone" type="audio/mpeg">
 </audio>