Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在图像上播放音频单击-移动/平板电脑_Javascript_Jquery_Html_Jquery Mobile_Audio - Fatal编程技术网

Javascript 在图像上播放音频单击-移动/平板电脑

Javascript 在图像上播放音频单击-移动/平板电脑,javascript,jquery,html,jquery-mobile,audio,Javascript,Jquery,Html,Jquery Mobile,Audio,我无法让我的代码在单击图像时播放音频文件。 我尝试了不同的文件类型,但没有任何改变 这是我正在使用的代码: var声音=[ "http://adambencreative.co.uk/wp-content/uploads/2013/12/test.mp3", "http://adambencreative.co.uk/wp-content/uploads/2013/12/test2.mp3", ], oldSounds=[]; var playSounds=函数(){ var inde

我无法让我的代码在单击图像时播放音频文件。 我尝试了不同的文件类型,但没有任何改变

这是我正在使用的代码:


var声音=[
"http://adambencreative.co.uk/wp-content/uploads/2013/12/test.mp3",
"http://adambencreative.co.uk/wp-content/uploads/2013/12/test2.mp3",
],
oldSounds=[];
var playSounds=函数(){
var index=Math.floor(Math.random()*(sounds.length)),
thisSound=声音[索引];
按一下(这个声音);
声音。拼接(索引,1);
if(sounds.length<1){
sounds=oldSounds.splice(0,oldSounds.length);
}
$(“#元素”).html(“”);
}

有人知道我如何让它也在手机/平板电脑上工作吗?

我只是简单地添加了
$('audio')[0].play()
元素添加到页面后,会播放声音。可能是自动播放功能有问题。对不起,你在哪里添加的代码如果你能复制你编辑的代码的精确摘录,那就太好了!或者使用我的代码制作另一个JSFIDLE,那会更好@BVX89试试这个:不用担心,解决了,非常感谢!你可能应该写下你做了什么来修复它,让别人看到。
<a onclick="playSounds()">
<img src="http://us.123rf.com/400wm/400/400/natis76/natis761205/natis76120500079/13695359-speaker-icon.jpg" width="300px" height="300px" id="ImageButton1">
    var sounds = [



"http://adambencreative.co.uk/wp-content/uploads/2013/12/test.mp3",
"http://adambencreative.co.uk/wp-content/uploads/2013/12/test2.mp3",


          ],
oldSounds = [];

var playSounds = function () {
var index = Math.floor(Math.random() * (sounds.length)),
    thisSound = sounds[index];

    oldSounds.push(thisSound);
    sounds.splice(index, 1);

    if (sounds.length < 1) {
        sounds = oldSounds.splice(0, oldSounds.length);
    }

 $("#element").html("<audio autoplay><source src=\"" + thisSound + "\" type=\"audio\/mp3\"><\/audio>");
 }