如果标题不对,我很抱歉,我不可能想到如何说出来。
我正在制作一个Chrome网络应用程序,它是一种声音板。现在定义音频并听一个按钮点击我一直这样做
//Variables for creating audio loads//
var heyboosong = new Audio();
var ricksong = new Audio();
var rapgod = new Audio();
//stopfunction has a button on the html that goes and will pause the sound playing. right now it's a list of every sound. I need to change it to just pause anything that plays//
function stopmusic() {
heyboosong.pause();
ricksong.pause();
rapgod.pause();
//audio functions to listen to the buttons and make sure they are clicked, if so then wohoo sound//
function myboo() {
heyboosong.src = "mp3/myboo.mp3";
heyboosong.play();
}
document.getElementById('myboobutton').addEventListener('click', myboo);
function rickplay() {
ricksong.src = "mp3/rick.mp3";
ricksong.play();
}
document.getElementById('rickbutton').addEventListener('click', rickplay);
function rapgodplay() {
rapgod.src = "mp3/rapgod.mp3";
rapgod.play();
}
现在这一切都很好,花花公子,但这是痛苦的。我有超过100种不同的声音,而不仅仅是这三种声音。所以我每次都必须输入所有内容。无论如何,我可以使用Jquery或其他方法来浓缩这个吗?
答案 0 :(得分:1)
我不知道它是否会奏效,但它会给你一些想法。如果你可以将歌曲标题存储在变量中,我认为它会起作用。
var mySong = new Audio();
function stopmusic() {
mySong.pause();
}
function getSong(title) {
mySong.src = "mp3/"+ title +".mp3";
mySong.play();
}
document.getElementById(title+'button').addEventListener('click', title);