微信小程序录音+播放
1、创建全局变量
const audioManager = wx.getRecorderManager();const innerAudioContext = wx.createInnerAudioContext();
2、录制
// 监听录制结束audioManager.onStop((res) => {// res.tempFilePath文件的本地存储地址})// 开始录制audioManager.start({duration: 600000, // 自定义时间});// 停止录制audioManager.stop();// 暂停录制audioManager.pause()// 结束录制audioManager.stop();
3、播放录音
// 监听音频播放失败innerAudioContext.onError((res) => {console.log(res)})// 监听音频播放结束innerAudioContext.onEnded(() => {})// 设置音频播放路径innerAudioContext.src = '本地路径或远程路径'// 开始播放音频innerAudioContext.play();// 暂停播放音频innerAudioContext.pause()// 设置播放位置innerAudioContext.seek(10)// 参数为数字类型 秒为单位// 继续播放innerAudioContext.play();
开发所遇到的问题
播放远程文件时 android 可以播放 ios 播放失败
1、检查支持格式
2、需要请求响应头里携带 range
与后端协商加上响应头后解决ios无法播放问题