我正在制作音频播放器。当页面加载音频文件开始播放时。一切都很好但是当音频文件播放时我想在音频播放/暂停按钮中添加“播放”类我有以下HTML代码
<div class="player">
<audio id="audio" src="audio/testaudio.mp3" autoplay=""></audio>
<div class="btn__play pull-left" id="btnplay">
<i class="play-pause-button"></i>
</div>
</div>
答案 0 :(得分:2)
附加playing
和pause
事件,并在那里执行操作。
var playBtn = document.getElementById('play');
var myAudio = document.getElementById("myAudio");
var isPlaying = false;
$(playBtn).click(function() {
if (isPlaying) {
myAudio.pause()
} else {
myAudio.play();
}
});
myAudio.onplaying = function() {
isPlaying = true;
$(playBtn).removeClass('ion-play');
$(playBtn).addClass('ion-pause');
};
myAudio.onpause = function() {
isPlaying = false;
$(playBtn).addClass('ion-play');
$(playBtn).removeClass('ion-pause');
};
button {
font-size: 22px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet" />
<body>
<h3>Audio Test: <button id="play" class="ion-play"></button></h3>
<div>
<audio id="myAudio" src="http://www.sousound.com/music/healing/healing_01.mp3" preload="auto">
</audio>
</div>
</body>
答案 1 :(得分:0)
您可以通过javascript轻松添加课程:
var playbutton = document.getElementById("btnplay");
playbutton.className += " playing";