【发布时间】:2020-02-05 06:17:42
【问题描述】:
我制作了一个简单的音频播放器,但现在我想添加一个歌曲进度条,在歌曲播放时填满时间线,这是我的 html:
<div id="ap-timeline" onClick={this.mouseMove} ref={(timeline) => { this.timeline = timeline }}>
<div id="ap-handle" onMouseDown={this.mouseDown} ref={(handle) => { this.handle = handle }} />
<div id="ap-handle-circle" onMouseDown={this.mouseDown} ref={(handleCircle) => { this.handleCircle = handleCircle }} />
</div>
这是我的 CSS:
#ap-timeline{
display: flex;
flex-direction: row;
align-items: center;
width: 550px;
height: 4px;
border-radius: 15px;
background: $audio-slider-gray;
margin: 0 10px;
#ap-handle {
background: $white;
height: 4px;
}
#ap-handle-circle {
width: 13px;
height: 13px;
border-radius: 50%;
background: $white;
transform: scale(0.25);
}
}
}
ap-handle 是我试图在歌曲播放时用来填充时间线的进度条。
【问题讨论】:
-
您是否将歌曲的长度存储在任何地方?
-
是的!它处于状态,可以通过我的组件中的
this.state.duration访问(这也是它呈现html的地方) -
您需要获取歌曲的当前时间、持续时间,并将其标准化为 0-100% 并将其应用于进度条。
-
@Phix 有道理,你能提供一个例子吗?作为参考,我可以通过
this.state.currentTime访问歌曲的当前时间,通过this.state.duration访问歌曲的持续时间。我猜你会找到某种比率(currentTime 与歌曲持续时间的百分比)。我不确定如何在 CSS 中使用它:( -
我的
componentDidMount函数中确实有这个比率,这似乎与您所说的接近:let ratio = this.audio.currentTime / this.audio.duration;