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

HTML audio player timeline functionality add

本文关键字:功能 时间 播放器 HTML 音频 添加      更新时间:2023-09-26

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

#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>

我使用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>

您需要在页面上添加Javascript代码才能拥有此功能。你可以从这个代码中提取你想要的代码(Nicely解释道(。

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

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

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