Lyric(sLyric) {
let line = sLyric.split(’\n’)
// console.log(line)
let _lrcList = []
line.forEach((elem) => {
let time = elem.match(/[(\d{2,})😦\d{2})(?:.(\d{2,3}))?]/g)
if (time != null) {
let lrc = elem.split(time)[1]
let timeReg = time[0].match(/(\d{2,})😦\d{2})(?:.(\d{2,3}))?/)
// console.log(timeReg)
// 把时间转换为秒
let time2Seconds = parseInt(timeReg[1]) * 60 + parseInt(timeReg[2]) + parseInt(timeReg[3]) / 1000
_lrcList.push({
lrc,
time: time2Seconds,
})
}
})
this.setData({
lrcList: _lrcList
})
}
}
子组件相互传值、
<x-lyric class="lyric" isLyricShow="{{!isPlayingshow}}" bind:tap="onChangeLyricShow" lyric="{{lrc}}" />
<view class="progress-bar"> <m bind:timeUpdate="timeUpdate"></m></view>
this.triggerEvent('timeUpdate', {
currentTime
})
timeUpdate(){
this.selectAllComponents('.lyric').update(event.detail.currentTime)
},
<scroll-view hidden="{{isLyricShow}}" class="lyric-scroll" scroll-y scroll-top="{{scrollTop}}" scroll-with-animation="true">
<view class="lyric-panel">
<block wx:for="{{lrcList}}" wx:key="item">
<view class="lyric {{index==nowLyricIndex?'hightlight-lyric': ''}}">{{item.lrc}}</view>
</block>
</view>
</scroll-view>
update(currentTime) {
let lrcList = this.data.lrcList
if (lrcList.length == 0) {
return
}
if (currentTime > lrcList[lrcList.length - 1].time) {
if (this.data.nowLyricIndex != -1) {
this.setData({
nowLyricIndex: -1,
scrollTop: lrcList.length * lyricHeight
})
}
}
for (let i = 0, len = lrcList.length; i < len; i++) {
if (currentTime <= lrcList[i].time) {
this.setData({
nowLyricIndex: i - 1,
scrollTop: (i - 1) * lyricHeight
})
break
}
}
},
lifetimes: {
ready() {
// 750rpx
wx.getSystemInfo({
success(res) {
// console.log(res)
// 求出1rpx的大小
lyricHeight = res.screenWidth / 750 * 64
},
})
},
},