牛骨文教育服务平台(让学习变的简单)

微信小程序API 音频播放控制


wx.playVoice(OBJECT)


开始播放语音,同时只允许一个语音文件正在播放,如果前一个语音文件还没播放完,将中断前一个语音播放。

OBJECT参数说明:

QQ截图20170208110700.png

示例代码:

wx.startRecord({
  success:function(res){
    var tempFilePath = res.tempFilePath;
    wx.playVoice({
      filePath:tempFilePath,
      complete:function(){
      } 
    })
  }
})

wx.pauseVoice()


暂停正在播放的语音。再次调用wx.playVoice播放同一个文件时,会从暂停处开始播放。如果想从头开始播放,需要先调用wx.stopVoice

示例代码:

wx.startRecord({
  success:function(res){
    var tempFilePath = res.tempFilePath;
      wx.playVoice({
      filePath: tempFilePath
    });

    setTimeout(function(){
        //暂停播放
      wx.pauseVoice()
    },5000)
  }
});

wx.stopVoice()


结束播放语音

示例代码:

wx.startRecord({
  success:function(res){
    var tempFilePath = res.tempFilePath;
    wx.playVoice({
      filePath:tempFilePath
    })

    setTimeout(function(){
      wx.stopVoice();
    },5000)
  }
});