音频组件audio和组件video类似,根据id使用wx.createAudioContext('myAudio')获得audio的上下文实例对象

audio组件的属性

image.png代码示例

  1. index.wxml
  2. ----------
  3. <view style='padding:20px;'>
  4. <audio
  5. poster="{{poster}}"
  6. name="{{name}}"
  7. author="{{author}}"
  8. src="{{src}}"
  9. id="myAudio"
  10. controls
  11. loop
  12. ></audio>
  13. <button type="primary" bindtap="audioPlay">播放</button>
  14. <button type="primary" bindtap="audioPause">暂停</button>
  15. <button type="primary" bindtap="audio14">设置当前播放时间为14秒</button>
  16. <button type="primary" bindtap="audioStart">回到开头</button>
  17. </view>
  18. ---------------------------------------------------------------------------------
  19. index.js
  20. ------------------
  21. Page({
  22. onReady(e) {
  23. // 使用 wx.createAudioContext 获取 audio 上下文 context
  24. this.audioCtx = wx.createAudioContext('myAudio')
  25. },
  26. data: {
  27. poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=
  28. 2592000',
  29. name: '此时此刻',
  30. author: '许巍',
  31. src: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd93
  32. 37d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8D
  33. DD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',
  34. },
  35. audioPlay() {
  36. this.audioCtx.play()
  37. },
  38. audioPause() {
  39. this.audioCtx.pause()
  40. },
  41. audio14() {
  42. this.audioCtx.seek(14)
  43. },
  44. audioStart() {
  45. this.audioCtx.seek(0)
  46. }
  47. })

image.png