单击图像时播放声音

时间:2015-02-17 17:58:54

标签: javascript wordpress html5-audio

我有一个wordpress页面,我希望在点击图像时播放声音。我从本教程获得了代码:

http://iwebfaq.org/site/iWeb_Sound_effects.html

基本上,我编辑了页面并在文本编辑器中添加了它:

    <script>
// Mouseover/ Click sound effect- by JavaScript Kit (www.javascriptkit.com)

var html5_audiotypes={ //define list of audio file extensions and their associated audio types. Add to it if your specified audio file isn't on this list:
"mp3": "audio/mpeg",
"mp4": "audio/mp4",
"ogg": "audio/ogg",
"wav": "audio/wav"
}
function createsoundbite(sound){
var html5audio=document.createElement('audio')
if (html5audio.canPlayType){ //check support for HTML5 audio
for (var i=0; i<arguments.length; i++){
var sourceel=document.createElement('source')
sourceel.setAttribute('src', arguments[i])
if (arguments[i].match(/\.(\w+)$/i))
sourceel.setAttribute('type', html5_audiotypes[RegExp.$1])
html5audio.appendChild(sourceel)
}
html5audio.load()
html5audio.playclip=function(){
html5audio.pause()
html5audio.currentTime=0
html5audio.play()
}
return html5audio
}
else{
return {playclip:function(){throw new Error("Your browser doesn't support HTML5 audio unfortunately")}}
}
}

var mouseoversound1=createsoundbite("http://mywebsite.com/wp-content/uploads/2015/02/island_music_x.mp3")
var clicksound1=createsoundbite("http://mywebsite.com/wp-content/uploads/2015/02/island_music_x.mp3")

</script>

之后我添加了这个:

<a nohref onclick="clicksound1.playclip()"><img src="http://mywebsite.com/wp-content/uploads/2015/02/ananas.png" style="width:126px;height126px;border:0px;"></a>

我做错了什么?当我应用它时没有任何反应,图像显示但点击它绝对没有什么

1 个答案:

答案 0 :(得分:0)

代码很好,我用oder mp3文件累了。 mp3源不存在(或url错误)。如果你更改它,脚本将工作。