【问题标题】:Playing a sound when clicking on an image单击图像时播放声音
【发布时间】:2015-02-17 17:58:54
【问题描述】:

我有一个 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>

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

【问题讨论】:

    标签: javascript wordpress html5-audio


    【解决方案1】:

    代码很好,我厌倦了一个 oder mp3 文件。 mp3 源不存在(或 url 错误)。如果你改变它,脚本就会工作。

    【讨论】:

    • 我已经检查了多次链接,但仍然无法正常工作。也许我的主题不支持 HTML5?我去看看
    • 可以肯定的是,我建议尝试使用 [banchieri.hu/krudy/02%20-%20Track%20%202.mp3] 。并检查,您使用 '' 启动 html 文件
    • 不,它不起作用...尝试了您的链接,如果在主题中用作单独的音频媒体文件,它可以正常工作,但没有任何反应。我在页面的开头添加了 并且还添加了 'add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list' ) );'到我的functons.php,因为它以前不存在......仍然没有。
    猜你喜欢
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    相关资源
    最近更新 更多