最近在开发小程序中,使用到了音频播放,如下:
- 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);
},