首頁  >  文章  >  微信小程式  >  小程式怎麼做音樂播放條

小程式怎麼做音樂播放條

angryTom
angryTom原創
2020-03-19 18:06:043185瀏覽

小程式怎麼做音樂播放條

小程式怎麼做音樂播放條   

#可以使用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 + &#39;&#39;,
              duration: res.duration
            })
          }
        })
      }, 1000)
    })

    // 监听背景音频暂停事件
    wx.onBackgroundAudioPause(() => {
      clearInterval(that.timer)
    })

    // 监听背景音频停止事件
    wx.onBackgroundAudioStop(() => {
      clearInterval(that.timer)
    })
  },
  
  playMusic() {
 	 let obj = {
       dataUrl: &#39;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&#39;,
        title: &#39;昨日青空&#39;,
        coverImgUrl: &#39;/static/images/avatar.png&#39;
      }
      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 + &#39;&#39;
        })
        this.data.current = (this.data.musicPercent/100)*this.data.duration
      }
    }
  },
  
  setProgress() {
    let that = this
    console.log(&#39;bindtouchend&#39;)
    wx.getBackgroundAudioPlayerState({
      success: res => {
        that.data.current !== res.currentPosition &&
        wx.seekBackgroundAudio({
          position: that.data.current,
          success () {
            console.log(&#39;seek&#39;, that.data.current)
          }
        })
      }
    })
  }

播放列的有效區域

##橫向: e.touches[0].clientX

縱向: e.touches[0].clientY

此處橫向為55~355 ,縱向為390~410

#定義觸控事件
##取得的橫向進度條位置,計算用戶所拖曳到的進度條位置

**注意:在此處呼叫wx.seekBackgroundAudio()設定播放進度,會導致音訊出現卡頓的效果。因為使用者拖曳的過程中會多次呼叫seek方法,所以應該把設定播放進度放在拖曳進度條完成之後再執行。

touchend監聽觸控事件的停止

根據觸控事件計算所得的時間current,呼叫wx.seekBackgroundAudio()設定播放進度

效果:

小程式怎麼做音樂播放條PHP中文網,大量

網站建立教學

,歡迎學習!

以上是小程式怎麼做音樂播放條的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn