微信小程序背景音乐开发详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

微信小程序背景音乐开发详解

前端_李嘉豪   2020-05-17 我要评论

wx.getBackgroundAudioManager() 

推荐大家使用背景音乐不在使用audio

audio播放音频加载时间长 体验需求达不到

相关文档

https:/https://img.qb5200.com/download-x/developers.weixin.qq.com/miniprogramhttps://img.qb5200.com/download-x/dev/api/media/background-audio/BackgroundAudioManager.html

相关代码

const bgMusic = wx.getBackgroundAudioManager() //创建背景音乐
Page({
 
 data: {
  id: '',//请求数据参数id
  musicMsg: {},//接收数据对象
 },
 
 onLoad: function (options) {
  var that = this
  that.setData({
   id: options.id,
  })
  that.getPicInfo() //请求服务器
 }, 
 // 开始播放
 listenerButtonPlay: function (src) {
  var that = this
  console.log(src)
  bgMusic.src = src;
  bgMusic.onTimeUpdate(() => { //监听音频播放进度
   console.log(bgMusic.currentTime)
  })
  bgMusic.onEnded(() => { //监听音乐自然播放结束
   console.log("音乐播放结束");
   that.listenerButtonPlay(that.data.musicMsg.mp3)
  })
  bgMusic.play(); //播放音乐
 },
 getPicInfo() {
  var that = this
  wx.request({
   url: 'https://xxxxx.xxxxx.com/1.json?id=' + that.data.id,
   header: {
    'content-type': 'application/json'
   },
   method: 'GET',
   success: function (res) {
    that.listenerButtonPlay(res.data.data.mp3) //成功回调执行播放音乐
    that.setData({
     musicMsg: res.data.data, //赋值对象
    })
 
   }
  })
 },
 
 onUnload() {
  var that = this
  that.listenerButtonStop()//页面卸载时停止播放
  console.log("离开")
 },
 
 //暂停
 audioPause: function () {
  var that = this
  bgMusic.pause(); //暂停播放音乐
  console.log('暂停')
 },
 audioPlay: function () {
  var that = this
  bgMusic.play(); //停止播放
  console.log('继续播放')
 },
 //停止播放
 listenerButtonStop: function () {
  bgMusic.stop()
 },
})

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们