- wx. playBackgroundAudio:播放背景音樂
- wx.pauseBackgroundAudio:暫停背景音樂
- wx.stopBackgroundAudio:中止背景音樂
- wx.seekBackgroundAudio:隨機定位背景音樂
<view style="margin: 30px;"> <view style="width: 100%;display: flex;"> <text style="width: 100%;font-size: 60px;margin: 20px; text-align: center; ">{{formatedPlayTime}}</text> </view> <slider style="width: 100%" min="0" max="401" step="1" value="{{playTime}}" bindchange="seek" ></slider> <view style="font-size: 28px; width: 100%;display: flex;justify-content: space-between;box-sizing: border-box;"> <text>00:00</text> <text>06:41</text> </view> <button style="margin-top:20px" bindtap="playBackgroundAudio">播放背景音樂</button> <button style="margin-top:20px" bindtap="pauseBackgroundAudio">暫停背景音樂</button> <button style="margin-top:20px" bindtap="stopBackgroundAudio">中止背景音樂</button></view>
// 背景音樂文件對應的Url(在線播放)var dataUrl ='http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46' var app = getApp()Page({ data: { playTime:0, // <slider>組件當前的位置 formatedPlayTime: '00:00:00' // 當前播放的位置 }, //播放背景音樂 playBackgroundAudio: function () { var that = this wx.playBackgroundAudio({ dataUrl: dataUrl, title: '此時此刻', }) this.enableInterval() app.globalData.backgroundAudioPlaying = true }, // 暫停背景音樂 pauseBackgroundAudio: function () { var that = this wx.pauseBackgroundAudio() app.globalData.backgroundAudioPlaying = false }, // 中止背景音樂 stopBackgroundAudio: function () { var that = this wx.stopBackgroundAudio({ success: function (res) { that.setData({ playTime: 0, formatedPlayTime: '00:00:00' }) } }) app.globalData.backgroundAudioPlaying = false }, // 將秒格式的時間格式化爲00:00:00形式(時分秒) formatTime: function (time) { if (typeof time !== 'number' || time < 0) { return time } var hour = parseInt(time / 3600) time = time % 3600 var minute = parseInt(time / 60) time = time % 60 var second = time return ([hour, minute, second]).map(function (n) { n = n.toString() return n[1] ? n : '0' + n }).join(':') }, // 隨機定位背景音樂 seek: function (e) { clearInterval(this.updateInterval) var that = this wx.seekBackgroundAudio({ position: e.detail.value, complete: function () { // 實際會延遲兩秒左右才跳過去 setTimeout(function () { that.enableInterval() }, 2000) } }) }, // 開啓定時器,播放計時 enableInterval: function () { var that = thisupdate()// 開啓定時器,500毫秒獲取一次背景音樂的播放位置 this.updateInterval = setInterval(update, 500) function update() { wx.getBackgroundAudioPlayerState({ success: function (res) { if (res.currentPosition != undefined) {that.setData({ playTime: res.currentPosition, formatedPlayTime: that.formatTime(res.currentPosition + 1) }) } } }) } },})
- wx.playBackgroundAudio方法支持在線播放背景音樂,經過dataUrl屬性指定在線背景音樂的Url
- 這裏使用app.globalData.backgroundAudioPlaying變量保存背景音樂的播放狀態。其中app.globalData是全局做用域。因爲背景音樂對於當前小程序來講是全局的,因此要求即便播放背景音樂的當前窗口關閉,播放狀態變量仍然有效,因此須要將這些相對於當前小程序是全局的變量放到app.globalData中
- 本例使用了JavaScript中標準的定時器來獲取背景音樂播放的當前位置,每500毫秒經過wx.getBackgroundAudioPlayerState方法獲取一次背景音樂播放的位置
本文分享自微信公衆號 - 極客起源(geekculture)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。javascript