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

标签 javascript jquery css html 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>

最佳答案

我使用 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 - HTML 音频播放器时间轴功能添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39094273/

相关文章:

JavaScript 渐进式未定义检查

javascript - 获取 jQuery 选择元素的值

css - 居中嵌套div

javascript - 使用 .NET/C# 与使用 HTML/JavaScript 构建 Android 应用程序

javascript - Angular 树组件不显示任何节点

javascript - 在无序列表中查找重复项

jquery - addClass() 然后在单击时removeClass() (jquery)

ajax - Grails 中的 jQuery Ajax 请求

javascript - 我的 anchor 标记没有采用 javascript 函数和 php 中的内联样式表

javascript - 通过创建实例来修改的类变量