学无先后,达者为师

网站首页 前端文档 正文

小程序音频播放js

作者:布偶乱跑 更新时间: 2022-04-10 前端文档

最近在开发小程序中,使用到了音频播放,如下:

  • js
//player.js
const innerAudioContext = wx.createInnerAudioContext()

// var PlayProcess;

// innerAudioContext.onPlay=function(){
//   PlayProcess();
// }

function Play(source) {
  innerAudioContext.stop()
  innerAudioContext.src = source
  innerAudioContext.play()
}

function Stop() {
  innerAudioContext.stop()
  innerAudioContext.src = null
}

module.exports = {
  Play: Play,
  Stop: Stop
  // PlayProcess: PlayProcess
}

  • wxml
    <view bindtap='playVoice' data-resource='{{item.resource}}'>
      <view>
        <image src='tupian.png' mode='aspectFit'></image> {{item.duration}}
      </view>
    </view>
  • js
const voicePlayer = require('./utils/player')

  /**
   * 播放语音按钮点击事件
   */
  playVoice: function(event) {
    var resource = event.currentTarget.dataset.resource;
    voicePlayer.Play(resource);
  },

原文链接:https://blog.csdn.net/weixin_44634372/article/details/123787507

栏目分类
最近更新