The old version

<! -- audio.wxml --><audio poster="{{poster}}" name="{{name}}" author="{{author}}" src="{{src}}" id="myAudio" controls loop></audio>

<button type="primary" bindtap="audioPlay">play</button>
<button type="primary" bindtap="audioPause">suspended</button>
<button type="primary" bindtap="audio14">Set the current playback time to 14 seconds</button>
<button type="primary" bindtap="audioStart">Back to the beginning</button>
Copy the code
// audio.js
Page({
  onReady: function (e) {
    Get the Audio context with wx.createAudioContext
    this.audioCtx = wx.createAudioContext('myAudio')},data: {
    poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000'.name: 'Here and now'.author: 'xu wei'.src: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51 E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',},audioPlay: function () {
    this.audioCtx.play()
  },
  audioPause: function () {
    this.audioCtx.pause()
  },
  audio14: function () {
    this.audioCtx.seek(14)},audioStart: function () {
    this.audioCtx.seek(0)}})Copy the code

The use of the new version: wx createInnerAudioContext

<! -- Currently stopped --><view style="width:250rpx; height:250rpx; left:250rpx; top:12rpx; position:relative;" wx:if="{{isplay==false}}" bindtap='play'>
  <image style="width:100%; height:100%; border-radius:50%;" src='http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000' />
  <image src='.. /image/play.png' style="width:100rpx; height:100rpx; position:absolute; left:75rpx; top:75rpx;"></image>
</view><! -- Currently in playback state --><view style="width:250rpx; height:250rpx; left:250rpx; top:12rpx; position:relative;" wx:if="{{isplay==true}}" bindtap='stop'>
  <image style="width:100%; height:100%; border-radius:50%;" src='http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000' />
  <image src='.. /image/stop.png' style="width:100rpx; height:100rpx; position:absolute; left:75rpx; top:75rpx;"></image>
</view>
<slider style="width:500rpx; margin-left:125rpx;"></slider>
Copy the code
const myaudio = wx.createInnerAudioContext();
Page({
 
  data: {
    isplay: false.// Whether to play
  },
  onShow: function () {
    myaudio.src = "http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51 E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46"
 
  },
  / / play
  play: function () {
 
    myaudio.play();
    console.log(myaudio.duration);
    this.setData({ isplay: true });
  },
  / / stop
  stop: function () {
    myaudio.pause();
    this.setData({ isplay: false}); }})Copy the code