HTML音频播放器时间轴功能添加

时间:2016-08-23 06:55:46

标签: javascript jquery css html5 audio

我正在尝试添加HTML音频播放器。我在那里应用了代码播放暂停工作。我需要在这里添加时间轴功能。但不知道该怎么做。这里还有另一个问题,我不知道如何在同一个按钮中应用播放暂停。请帮我。以下是我的代码。 sample

#audioplayer{
	width: 480px;
	height: 60px;
	margin: 50px auto auto auto;
border: solid;
}

#pButton{
    height:60px; 
    width: 60px;
    border: none;
    background-size: 50% 50%;
    background-repeat: no-repeat;
    background-position: center;
    float:left;
    outline:none;
}

.play, .play:hover, .pause:focus{background: url('https://img.pranavc.in/20') ;}
.pause, .pause:hover, .play:focus{background: url('https://img.pranavc.in/30') ;}

#timeline{
	width: 400px;
	height: 20px;
	margin-top: 20px;
	float: left;
	border-radius: 15px;
	background: rgba(0,0,0,.3);
  
}
#playhead{
	width: 18px;
	height: 18px;
	border-radius: 50%;
	margin-top: 1px;
	background: rgba(0, 0, 0,1);

}
<audio id="player" src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"></audio>
<div id="audioplayer">
 <button id="pButton" class="play" onclick="document.getElementById('player').play()"></button> 
  <button id="pButton" class="pause" onclick="document.getElementById('player').pause()"></button> 
	<div id="timeline">    
		 <div id="playhead"></div>
	</div>
</div>

3 个答案:

答案 0 :(得分:1)

我使用javascript,并更改dom和css。 你需要学习音频事件和财产。

var audio = document.getElementById("player");
var btn = document.getElementById("pButton");
btn.addEventListener("click", function(){
   if (audio.paused) {
   	   audio.play();
       btn.classList.remove("pause");
       btn.classList.add("play");
   } else  {
   	   audio.pause();
       btn.classList.remove("play");
       btn.classList.add("pause");
   }
});
var playhead = document.getElementById("playhead"); audio.addEventListener("timeupdate", function(){ var duration = this.duration; var currentTime = this.currentTime; var percentage = (currentTime / duration) * 100; playhead.style.left = percentage * 4 + 'px'; });
#audioplayer{
    position: relative;
	width: 480px;
	height: 60px;
	margin: 50px auto auto auto;
    border: solid;
}

#pButton{
    height:60px; 
    width: 60px;
    border: none;
    float:left;
    outline:none;
}

#pButton.pause {
    background: url('https://img.pranavc.in/20') no-repeat;
    background-size: 56px;
    background-position: center;
}

#pButton.play {
    background: url('https://img.pranavc.in/30') no-repeat;
    background-size: 56px;
    background-position: center;
}

#timeline{
	width: 400px;
	height: 20px;
	margin-top: 20px;
	float: left;
	border-radius: 15px;
	background: rgba(0,0,0,.3);
    position: relative;
}
#playhead{
	width: 18px;
	height: 18px;
	border-radius: 50%;
	margin-top: 1px;
	background: rgba(0, 0, 0,1);
    position: absolute;
    left: 0px;
}
<audio id="player" src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"></audio>
<div id="audioplayer">
 <button id="pButton" class="pause"></button>
 <div id="timeline">    
	<div id="playhead"></div>
 </div>
</div>

答案 1 :(得分:0)

您需要在页面上添加Javascript代码才能拥有此功能。您可以从此代码中提取所需的代码(很好地解释)。

https://www.developphp.com/video/JavaScript/Audio-Play-Pause-Mute-Buttons-Tutorial

https://www.developphp.com/video/JavaScript/Audio-Seek-and-Volume-Range-Slider-Tutorial

答案 2 :(得分:0)

<audio controls>
    <source src="Link.mp3" type="audio/mpeg">
</audio>

尝试上述代码。 注意: Internet Explorer 8及更早版本不支持音频标记。 请检查此链接:http://www.w3schools.com/tags/tag_audio.asp