小程式怎麼做音樂播放條
#可以使用progress元件實作音樂播放條,具體做法如下:
# 1.新增一個audio標籤,不指定controls="true"屬性即可隱藏。
<view class="audio-play"> <audio src=""></audio> </view> <view> <view class="one-column play-it" bindtap="playMusic"> <view>点击播放</view> </view> <progress class="music-prog" bindtouchmove="setTouchMove" percent="{{musicPercent}}"></progress> <view class="percent-num">{{musicPercent}}%</view> </view>
bindtouchmove表示觸控事件;
progress標籤透過percent屬性設定進度
2、寫WSS檔案
#推薦學習:小程式開發
.play-it{ margin-left: 300rpx; } .music-prog{ width: 550rpx; height: 10rpx; margin: 50rpx 100rpx; color: #0099ff; background-color: #999; } .percent-num{ margin: -20rpx 0 0 100rpx; font-size: 28rpx; }
3、寫js,控製播放條。
onShow() { // 监听音乐播放 let that = this wx.onBackgroundAudioPlay(() => { that.timer && clearInterval(that.timer) that.timer = setInterval(() => { wx.getBackgroundAudioPlayerState({ success: res => { let per = (res.currentPosition/res.duration)*10000 that.setData({ musicPercent: Math.round(per)/100 + '', duration: res.duration }) } }) }, 1000) }) // 监听背景音频暂停事件 wx.onBackgroundAudioPause(() => { clearInterval(that.timer) }) // 监听背景音频停止事件 wx.onBackgroundAudioStop(() => { clearInterval(that.timer) }) }, playMusic() { let obj = { dataUrl: 'http://p6jceeddp.bkt.clouddn.com/%E5%B0%A4%E9%95%BF%E9%9D%96%20-%20%E6%98%A8%E6%97%A5%E9%9D%92%E7%A9%BA.mp3', title: '昨日青空', coverImgUrl: '/static/images/avatar.png' } wx.playBackgroundAudio(obj) }, setTouchMove (e) { if(e.touches[0].clientY >= 390 && e.touches[0].clientY <= 410) { if (e.touches[0].clientX >= 55 && e.touches[0].clientX <= 355) { let percent = (e.touches[0].clientX - 55)/300*10000 this.setData({ musicPercent: Math.round(percent)/100 + '' }) this.data.current = (this.data.musicPercent/100)*this.data.duration } } }, setProgress() { let that = this console.log('bindtouchend') wx.getBackgroundAudioPlayerState({ success: res => { that.data.current !== res.currentPosition && wx.seekBackgroundAudio({ position: that.data.current, success () { console.log('seek', that.data.current) } }) } }) }
播放列的有效區域
##橫向: e.touches[0].clientX縱向: e.touches[0].clientY此處橫向為55~355 ,縱向為390~410
#定義觸控事件
##取得的橫向進度條位置,計算用戶所拖曳到的進度條位置
**注意:在此處呼叫wx.seekBackgroundAudio()設定播放進度,會導致音訊出現卡頓的效果。因為使用者拖曳的過程中會多次呼叫seek方法,所以應該把設定播放進度放在拖曳進度條完成之後再執行。
touchend監聽觸控事件的停止根據觸控事件計算所得的時間current,呼叫wx.seekBackgroundAudio()設定播放進度
效果:
PHP中文網,大量
以上是小程式怎麼做音樂播放條的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境