场景
在点击播放音乐之后,歌曲的信息也要渲染到DOM中,其中关于歌曲的时长以及播放的时长也要添加上去。在信息渲染之后还要增加底部的功能,上/下一曲,播放/暂停,以及音量调节和播放模式等
步骤
歌曲显示
- wy-player.component.ts – 部分
duration: number; // 总时长
currentTime: number; // 当前已听时间
// 监听当前歌曲
private watchCurrentSong (song: Song) {
if (song) {
this.currentSong = song;
// 返回值中的dt为毫秒,处理为秒
this.duration = song.dt / 1000;
}
}
// 点击播放
onCanPlay () {
this.play();
}
// 更新已播放时长
onTimeUpdate (e: Event) {
this.currentTime = (<HTMLAudioElement>e.target).currentTime;
}
// 播放回调
private play () {
this.audioEl.play();
}
// 取值器-获取当前歌曲的小图标URL
get picUrl (): string {
return this.currentSong ? this.currentSong.al.picUrl : 'http://s4.music.126.net/style/web2/img/default/default_album.jpg';
}
}
- wy-player.component.html
<div class="m-player">
<div class="container">
<div class="wrap">
<div class="head">
<img [src]="picUrl" >
<i class="mask"></i>
</div>
<div class="play">
<div class="words clearfix " >
<p class="ellipsis margin-bottom-none">{
{currentSong?.name}}</p>
<ul class="songs clearfix margin-bottom-none ">
<li *ngFor="let item of currentSong?.ar; last as isLast">
<a>{
{item.name}}</a>
<span [hidden]="isLast">/</span>
</li>
</ul>
</div>
<div class="bar">
<span class="time">
<em>{
{currentTime | formatTime}}</em> / {
{duration | formatTime}}
</span>
</div>
</div>
</div>
</div>
<audio #audio [src]="currentSong?.url" (canplay)="onCanPlay()" (timeupdate)