企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
``` // components/classic/music/index.js const playSong = wx.getBackgroundAudioManager(); Component({ /** * 组件的属性列表 */ properties: { image: String, content: String, url: String }, /** * 组件的初始数据 */ data: { isPlay: false }, /** * 组件的方法列表 */ methods: { playMusic() { /*执行播放音乐函数时,如果isPlay值为true即正在播放,则执行playSong.pause()停止播放,更改相关参数值*/ if (this.data.isPlay) { playSong.pause(); this.setData({ isPlay: false }); } //如果沒有播放则更改isPlay的状态则音乐就会播放,然后找到该音乐的地址,title,以及切换音乐背景图片 else { playSong.title = this.properties.title, playSong.src = this.properties.url, // console.log(title); this.setData({ isPlay: true }); } }, /* 切换页面时播放按钮不变 */ _recoveryMusic() { if (playSong.src == this.properties.url) { this.setData({ isPlay: true }) } if (playSong.paused) { this.setData({ isPlay: false }) } } }, attached() { this._recoveryMusic(); //点播放,开始 playSong.onPlay(() => { this.setData({ isPlay: true }) }) //点暂停,不播放 playSong.onPause(() => { this.setData({ isPlay: false }) }) //关掉的时候,不播放 playSong.onStop(() => { this.setData({ isPlay: false }) }) //歌曲放完的时候,自动停止 playSong.onEnded(() => { this.setData({ isPlay: false }) }) } }) ```