您好我想使用点击播放音频我设法使用Javascript播放一个音频,但我需要在点击特定按钮时播放不同的音频。
我可以播放第一个,但第二个也播放相同的音频。我是Javascript的新手
function play() {
var audio = document.getElementById("sound1");
audio.play();
}
function play2() {
var audio2 = document.getElementById("sound2");
audio.play();
}
<audio id="sound1" src="https://feeds.soundcloud.com/stream/303404685-hopperkremer-w1-1.mp3"></audio>
<audio id="sound2" src="http://feeds.soundcloud.com/stream/303404681-hopperkremer-w4-1.mp3"></audio>
<input type="button" value="play" onclick="play()">
<input type="button" value="play2" onclick="play()">
(codepen)
答案 0 :(得分:1)
这是一个有效的例子:
systemd-journal-remote
&#13;
function play(){
var audio = document.getElementById("sound1");
audio.play();
}
function play2(){
var audio2 = document.getElementById("sound2");
audio2.play();
}
&#13;
答案 1 :(得分:1)
在这里尝试这个我还添加了暂停方法,如果有人试图在播放其他音频的同时播放一个音频,当前正在运行的音频会暂停,新的音频会播放
<audio id="sound1" src="https://feeds.soundcloud.com/stream/303404685-hopperkremer-w1-1.mp3"></audio>
<audio id="sound2" src="http://feeds.soundcloud.com/stream/303404681-hopperkremer-w4-1.mp3"></audio>
<input type="button" value="play" onclick="play()">
<input type="button" value="play2" onclick="play2()">
&#13;
var audio = new Audio('https://feeds.soundcloud.com/stream/303404685-hopperkremer-w1-1.mp3')
var audio2 = new Audio('http://feeds.soundcloud.com/stream/303404681-hopperkremer-w4-1.mp3')
function play() {
audio2.pause();
audio.play();
}
function play2() {
audio.pause();
audio2.play();
}
&#13;
答案 2 :(得分:0)
更改为:
<input type="button" value="play" onclick="play()">
<input type="button" value="play2" onclick="play2()">
同时将audio.play()
更改为audio2.play()
:
function play() {
var audio = document.getElementById("sound1");
audio.play();
}
function play2() {
var audio2 = document.getElementById("sound2");
audio2.play();
}
答案 3 :(得分:0)
play()
,将其更改为play2()
<input type="button" value="play" onclick="play()">
<input type="button" value="play2" onclick="play2()">