基於正則表示式的Java爬蟲專案
阿新 • • 發佈:2022-04-19
音樂播放器元件
audio.wxml
<view class="music"> <view class="music-img" bindtap="playMusic"> <image src="/static/blue_bck.png"></image> <image src="{{isPlaying?'/static/audio/play.png':'/static/audio/start.png'}}"></image> </view> <view class="time-img" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd"> <image src="/static/audio/gray_time.png"></image> <view class="act-img" style="width:{{audioWidth}}"></view> </view> <view class="time-text"> <text>{{duration}}</text> </view> </view>
audio.wxss
.music{ width: 700rpx; height: 126rpx; margin: 0 auto; padding-left: 16rpx; display: flex; flex-direction: row; align-items: center; border-radius: 5rpx; background-color: #F6F6F6; } .music-img{ position: relative; width: 120rpx; height: 96rpx; margin-right: 10rpx; display: flex; align-items: center; justify-content: center; } .music-img > image:first-of-type{ position: absolute; left: 0; top: 0; width: 120rpx; height: 96rpx; } .music-img > image:nth-of-type(2){ position: relative; width: 47rpx; height: 47rpx; } .time-img{ position: relative; width: 482rpx; height: 64rpx; overflow: hidden; } .time-img > image{ width: 482rpx; height: 64rpx; } .act-img{ position: absolute; left: 0; top: 0; width: 100rpx; height: 64rpx; background: url('https://qmjy.bingying.ren/static/wx/audio/blue_time.png') no-repeat; background-size: 482rpx 64rpx; z-index: 2; } .time-text{ margin-left: auto; width: 57rpx; height: 30rpx; line-height: 30rpx; text-align: center; border-top-left-radius: 13rpx; border-bottom-left-radius: 13rpx; background-color: #1F1E31; font-size: 16rpx; color: #fff; }
audio.js
Component({ /** * 元件的屬性列表 */ properties: { audioSrc: String, startPageX: Number, //開始的pageX 172 endPageX: Number, //結束的pageX 654 }, /** * 元件的初始資料 */ data: { audioWidth: 0, //播放進度的長 isPlaying: false, //是否正在播放 audioContent: null, //音訊物件 currentTime: 0, //播放當前時間(處理後的 1:00) duration: 0, //播放總時長 (處理後 4:30) oriCurrentTime: 0, //播放當前時間 s oriDuration: 0, //播放總時長 s touchStartX: 0, //觸控開始pageX }, lifetimes: { detached: function() { // 在元件例項被從頁面節點樹移除時執行 }, }, pageLifetimes: { // show: function () { // let that = this; // that.initMusic(); // }, hide: function () { let that = this; that.data.audioContent.stop(); }, }, ready: function() { // 在元件例項進入頁面節點樹時執行 let that = this; that.initMusic(); }, /** * 元件的方法列表 */ methods: { initMusic: function () { // 音訊 var that = this; wx.getSystemInfo({ success: function (res) { that.setData({ startPageX: res.windowWidth / 750 * that.properties.startPageX, endPageX: res.windowWidth / 750 * that.properties.endPageX }) }, }) let audioContent = wx.createInnerAudioContext(); audioContent.src = that.properties.audioSrc; that.setData({ audioContent: audioContent }) // 播放時間更新時 audioContent.onTimeUpdate(function () { if (that.data.oriDuration != 0) { let currentTime; let oriCurrentTime = parseInt(audioContent.currentTime); that.setData({ currentTime: that.handle(oriCurrentTime), oriCurrentTime: parseInt(audioContent.currentTime), audioWidth: oriCurrentTime / that.data.oriDuration * 100 + '%' }) } }) // 播放結束時 audioContent.onEnded(function () { that.setData({ isPlaying: false, currentTime: 0, audioWidth: '0%' }) }) // 音訊初始化完成設定時長 audioContent.onCanplay(function () { audioContent.duration; setTimeout(() => { that.setData({ duration: that.handle(parseInt(audioContent.duration)), oriDuration: parseInt(audioContent.duration) }) }, 1000) }) }, // 暫停播放點選 playMusic: function () { var that = this; if (that.data.duration != 0) { if (that.data.isPlaying) { that.setData({ isPlaying: false }) that.data.audioContent.pause(); } else { that.setData({ isPlaying: true }) that.data.audioContent.play(); } } }, // 秒變分鐘 handle: function (time) { return parseInt(time / 60) + ':' + ((time % 60) < 10 ? '0' + (time % 60) : (time % 60)); }, touchStart: function (e) { // console.log("x:" + e.touches[0].pageX + "Y:" + e.touches[0].pageY); var that = this; that.toSeek(e.touches[0].pageX); }, touchMove: function (e) { var that = this; that.toSeek(e.touches[0].pageX); // that.setData({ // touchStartX: e.touches[0].pageX // }) }, touchEnd: function () { var that = this; that.toSeek(that.data.touchStartX); }, toSeek: function (pageX) { var that = this; // let playPercent = pageX / that.data.endPageX; if (pageX > that.properties.endPageX) { pageX = that.properties.endPageX; } if (pageX < that.properties.startPageX) { pageX = that.properties.startPageX; } let playPercent = (pageX - that.properties.startPageX) / (that.properties.endPageX - that.properties.startPageX); // console.log(playPercent); that.setData({ touchStartX: pageX, audioWidth: playPercent * 100 + '%', duration: that.handle(parseInt(that.data.audioContent.duration)), oriDuration: parseInt(that.data.audioContent.duration) }) let oriCurrentTime = parseInt(that.data.oriDuration * playPercent); that.data.audioContent.seek(oriCurrentTime); that.setData({ oriCurrentTime: oriCurrentTime, currentTime: that.handle(oriCurrentTime) }) }, } })
呼叫
<my-audio id="myAudio" audioSrc="{{info.url}}" startPageX="172" endPageX="654"></my-audio>
效果