录音wxml代码片段
<button type="default" bindtouchstart="audiostart" bindtouchend="audioend" class="btn"> 按住说话 </button>
录音 js代码片段
const recorderManager = wx.getRecorderManager();
audiostart:function(){
wx.showToast({
title: '正在录音...',
icon: 'loading',
duration: 10000
});
recorderManager.start({
format: 'mp3'
});
},
audioend:function(){
wx.hideToast({
title: '正在录音...',
icon: 'loading'
});
var path = "";
var that = this;
console.log("结束1")
recorderManager.stop();
recorderManager.onStop(function(res){
path = res.tempFilePath;
console.log("结束")
console.log(res.tempFilePath )
if(res.tempFilePath!=""){
that.saveVoiceInfo(res.tempFilePath);
}else{
}
});
},
saveVoiceInfo:function(path){
var that = this;
wx.uploadFile({
url:'127.0.0.1:8080/saveVoiceInfo',
filePath: path,
name: 'file',
header: {
'content-type': 'multipart/form-data'
},
formData: {
'type' : 'mp3',
},
success: function (res) {
},
})
}
录音保存java后台代码
@ApiOperation(value = "语音保存", response = Map.class)
@PostMapping("saveVoiceInfo")
@IgnoreAuth
@ResponseBody
public Object updatefileimageinfos(HttpServletResponse response, HttpServletRequest request,
@RequestParam(value = "file", required = false) MultipartFile imagefile) throws IOException {
Map<String,Object> result = new HashMap<>();
String filetype = "";
String[] filetypearr = request.getParameterMap().get("type");
if (filetypearr.length>0){
filetype = filetypearr[0];
}
Map<String, Object> entity= new HashMap<>();
String dateFormat = new java.text.SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
String type = "";
try {
type = dateFormat+Tools.getFourUUID()+imagefile.getOriginalFilename().substring(imagefile.getOriginalFilename().lastIndexOf(".")).toLowerCase();
File targetFile = new File("D:/voice", type);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
imagefile.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
return resultid;
}
保存图片到本地
![](https://img-blog.csdnimg.cn/e6dd835963ba4dd8b168cc49913d79fa.jpeg)
读取录音
读取录音wxml代码片段
<view class="speak_me audio-player" wx:if="{{ item.path != '' }}" bindtap="play" data-play="{{item.say}}" data-path="{{item.path}}">
<text class="time">{{item.time}}</text>
<view
class="controls play-btn" bindtap="audioPlay">
<image style="margin-left{{item.timelength}}px"src="../../../static/images/voicemsg.png"></image>
</view>
</view>
读取录音js代码片段
play:function(e){
debugger
wx.showLoading({
title: '音频下载中',
})
var path=e.currentTarget.dataset.path;
var pathAll = "127.0.0.1:8080/voice/"+path;
wx.downloadFile({
url: pathAll,
success: res => {
console.log('开始播放')
console.log(res.tempFilePath);
this.innerAudioContext.src = res.tempFilePath
this.innerAudioContext.play()
wx.hideLoading()
},
fail: res => {
wx.hideLoading()
wx.showToast({
title: '出错了',
})
}
})
},
效果图
![请添加图片描述](https://img-blog.csdnimg.cn/234fd1fb96924124b8149b89dd871de9.jpeg)