我目前通过我的网站播放音乐的方法是HTML音频标签。但是,我希望能够通过HTML按钮来播放它。这个按钮应该能够在播放和停止之间切换音乐。我在JSFiddle上创建了一个示例,但不知道如何实现它。有人能用我的JSFiddle例子告诉我怎么做吗?
JSFiddle: http://jsfiddle.net/jTh3v/332/
--我最初的做法是:
document.getElementById("soundTag").innerHTML = "<audio controls volume preload='none' src='http://www.sousound.com/music/healing/healing_01.mp3'></audio>";发布于 2016-08-26 08:48:49
您可以通过单击event...insert,html.write上的一个按钮来播放声音,并在您的按钮上调用它作为onclick事件。
function playMusic(){
var music = new Audio('musicfile.mp3');
music.play();
}<input type="button" value="sound" onclick="playMusic()" />
确保给出一个有效的文件名。
发布于 2016-08-26 08:45:11
这将起作用:
document.getElementById("soundTag").innerHTML = "<audio controls volume preload='none' src='http://www.sousound.com/music/healing/healing_01.mp3'></audio>";
$('#button_play').on('click', function() {
//I added this
$("audio")[0].play();
$('#button_pause').show();
$('#button_play').hide();
});
$('#button_pause').on('click', function() {
//I added this
$("audio")[0].pause();
$('#button_play').show();
$('#button_pause').hide();
});.second {
display: none;
}<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Instead of doing the audio tag method, I want to do it through the buttons. However I don't know how to do this.</p><br>
<p>HTML Audio tag method (the method I don't want):</p>
<div id="soundTag"></div><br>
<p>Button method (the method I want, but doesn't work):</p>
<div>
<button id="button_play" class="first" type="button">
<i class="glyphicon glyphicon-volume-up"></i></button>
<button id="button_pause" class="second" type="button">
<i class="glyphicon glyphicon-volume-off"></i></button>
</div>
发布于 2020-11-02 14:46:31
这个成功了。删除我的mp3文件并上传您自己的mp3文件。
<button id="ASong" onClick="playPause()">
<audio
src="file_example_MP3_700KB.mp3"
autoplay
loop
></audio>
Song
</button>
<script>
var aud = document.getElementById("ASong").children[0];
var isPlaying = false;
aud.pause();
function playPause() {
if (isPlaying) {
aud.pause();
} else {
aud.play();
}
isPlaying = !isPlaying;
}
</script>https://stackoverflow.com/questions/39161487
复制相似问题