限时免费试用:欢迎注册 api.bigmodel.org ,快速体验大模型 API 接入服务。
当前位置:首页 >开发者 >网站框架 >小程序

小程序 | 背景音乐

分类:小程序时间:2018-09-10浏览:2267
//实例化
const mMgr = wx.getBackgroundAudioManager()


import { classicBeh } from '../classic-beh.js'
const mMgr = wx.getBackgroundAudioManager()
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [classicBeh],
  properties: {
    src:String
  },

  /**
   * 组件的初始数据
   */
  data: {
    playing:false,
    pauserSrc:'images/[email protected]',
    playSrc:'images/[email protected]'
  },
//暂停音乐
  detached:function(event){
    // mMgr.stop()
  },
  attached:function(event){
    this._recoverStatus()
    this._monitorSwitch()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onPlay:function(event){
      if(!this.data.playing){
        this.setData({
          playing: true
        })
        mMgr.src = this.properties.src
      }else{
        this.setData({
          playing: false
        })
        mMgr.pause()
      }
    },
    _recoverStatus: function () {
      //判断当前播放状态
      if (mMgr.paused) {
        this.setData({
          playing: false
        })
        return
      }
      //判断是否是当前的音乐
      if (mMgr.src == this.properties.src) {
        this.setData({
          playing: true
        })
      }
    },
    //监听事件(让主控开关控制播放图标)
    _monitorSwitch:function(){
      //播放
      mMgr.onPlay(()=>{
        this._recoverStatus()
      })
      // 暂停
      mMgr.onPause(() => {
        this._recoverStatus()
      })
      //主控开关上的×
      mMgr.onStop(() => {
        this._recoverStatus()
      })
      // 让音乐自动播放完成
      mMgr.onEnded(() => {
        this._recoverStatus()
      })
    }
  }
  
})


本站文章如未注明出处均为原创,转载请注明出处,如有侵权请邮件联系站长。
0/500
Share your thoughts respectfully.