您当前的位置: 首页 > 知识百科 > 怎么开发微信小程序播放音频功能?

怎么开发微信小程序播放音频功能?

时间:2023-07-01 14:05 阅读数:113 人阅读 分类:知识百科

  想要使用微信小程序播放音频功能,必须要进行相应的小程序开发。一般来说,只要使用小程序音频组件,就可以轻松的在小程序中播放音频了。一起来了解下微信小程序播放音频要怎么开发吧!

  属*名类型默认值说明

  idString video 组件的唯一标识符,

  srcString 要播放音频的资源地址

  loopBooleanfalse是否设置小程序循环播放

  controlsBooleantrue是否显示默认控件

  posterString 默认控件上的音频封面的图片资源地址,如果 controls 属*值为 false 则设置 poster 无效

  nameString未知音频默认控件上的音频名字,如果 controls 属*值为 false 则设置 name 无效

  authorString未知作者默认控件上的作者名字,如果 controls 属*值为 false 则设置 author 无效

  binderrorEventHandle 当发生错误时触发 error 事件,detail = {errMsg: MediaError.code}

  bindplayEventHandle 当开始/继续播放时触发play事件

  bindpauseEventHandle 当暂停播放时触发 pause 事件

  bindtimeupdateEventHandle 当播放进度改变时触发 timeupdate 事件,detail = {currentTime, duration}

  bindendedEventHandle 当播放到末尾时触发 ended 事件

  错误返回码:MediaError.code

  返回错误码 描述

  返回错误码描述

  MEDIA_ERR_ABORTED获取资源被用户禁止

  MEDIA_ERR_NETWORD网络错误

  MEDIA_ERR_DECODE解码错误

  MEDIA_ERR_SRC_NOT_SUPPOERTED不合适资源

  wx.createAudioContext(audioId)

  创建并返回audio上下文audioContext对象,控制音频的播放暂停与跳转。

  方法参数说明

  play无播放

  pause无暂停

  seekposition小程序跳转到指定位置,单位 s

  wxml

  播放

  暂停

  设置当前播放时间为14秒

  回到开头

  js

  Page({

  onReady: function (e) {

  // 使用 wx.createAudioContext 获取 audio 上下文 context

  this.audioCtx = wx.createAudioContext('myAudio')

  },

  data: {

  poster: 'y.gtimg/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',

  name: '此时此刻',

  author: '许巍',

  src: 'ws.stream.qqmusic.qq/M500001VfvsJ21xFqb.mp3?guid=ffffffff82

  },

  audioPlay: function () {

  this.audioCtx.play()

  },

  audioPause: function () {

  this.audioCtx.pause()

  },

  audio14: function () {

  this.audioCtx.seek(14)

  },

  audioStart: function () {

  this.audioCtx.seek(0)

  },

  funplay: function(){

  console.log("audio play");

  },

  funpause: function(){

  console.log("audio pause");

  },

  funtimeupdate: function(u){

  console.log(u.detail.currentTime);

  console.log(u.detail.duration);

  },

  funended: function(){

  console.log("audio end");

  },

  funerror: function(u){

  console.log(u.detail.errMsg);

  }

  })

  效果

  .wxml

  播放

  暂停

  调为2倍速

  调为1倍速

  调为0.5倍速

  设置当前播放时间为14秒

  回到开头

  .js

  Page({

  data: {

  poster: 'pic.pimg.tw/pam86591/1408719752-3322564110_n.jpg',

  name: 'Sugar',

  author: 'Maroon 5'

  },

  audioPlay: function () {

  this.setData({

  action: {

  method: 'play'

  }

  });

  },

  audioPause: function () {

  this.setData({

  action: {

  method: 'pause'

  }

  });

  },

  audioPlaybackRateSpeedUp: function () {

  this.setData({

  action: {

  method: 'setPlaybackRate',

  data: 2//加快速度

  }

  });

  },

  audioPlaybackRateSlowDown: function () {

  this.setData({

  action: {

  method: 'setPlaybackRate',

  data: 0.5//小于零放慢速度

  }

  });

  },

  audio14: function () {

  this.setData({

  action: {

  method: 'setCurrentTime',

  data: 14

  }

  });

  },

  audioStart: function () {

  this.setData({

  action: {

  method: 'setCurrentTime',

  data: 0

  }

  });

  }

  })

  效果

  上文中详细介绍了微信小程序播放音频功能的开发流程,小编还提供了在播放音频的过程中加速的方式,如果大家觉得这份资料有用的话就点击收藏一下吧,还有其他需要了解的内容请在微信小程序商店搜索。

  

  微信小程序音频直播开发源码

  小程序如何发布音频,小程序发布音频具体步骤

  微信小程序音频开发