本文实例为大家分享了小程序录音上传的具体代码,供大家参考,具体内容如下
demo.wxml
<view> <button bindtap='startRecordMp3' type='primary'>录音开始(mp3)</button> </view> <view> <button bindtap='stopRecord' type='primary'>录音结束</button> </view> <view> <button bindtap='playRecord' type='primary'>播放录音</button> </view> <view> <button bindtap='sendRecord' type='primary'>播放录音</button> </view>
demo.wxss
view{ padding: 15px; }
demo.js
// pages/newMusic/index.js const recorderManager = wx.getRecorderManager(); Page({ data: { }, /** * 提示 */ tip: function (msg) { wx.showModal({ title: '提示', content: msg, showCancel: false }) } /** * 录制mp3音频 */ , startRecordMp3: function () { recorderManager.start({ format: 'mp3' }); } /** * 停止录音 */ , stopRecord: function () { recorderManager.stop() } /** * 播放录音 */ , playRecord: function () { var that = this; var src = this.data.src; if (src == '') { this.tip("请先录音!") return; } this.innerAudioContext.src = this.data.src; this.innerAudioContext.play() }, onLoad: function (options) { var that = this; recorderManager.onError(function () { that.tip("录音失败!") }); recorderManager.onStop(function (res) { that.setData({ src: res.tempFilePath }) console.log(res.tempFilePath) that.tip("录音完成!") }); this.innerAudioContext = wx.createInnerAudioContext(); this.innerAudioContext.onError((res) => { that.tip("播放录音失败!") }) } })
java后台接收
package com.azor.controller; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import com.azor.utils.HttpAPIService; import ch.qos.logback.classic.Logger; import net.sf.json.JSONObject; @RestController @RequestMapping("/base_voice") public class BaseController { private static final Logger logger = (Logger) LoggerFactory.getLogger(BaseController.class); private static String lineSeparator = System.getProperty("line.separator"); @Autowired protected Environment env; @Autowired protected HttpAPIService httpAPIService; /** 上传文件保存路径 */ private final String FILE_SAVE_PATH = "D:/photo/jac_hr_miniprogram_file/"; /** 主业务数据Map */ protected Map<String, Object> dataMap = new HashMap<>(); /** HTTP POST 请求Map */ protected Map<String, Object> postMap = new HashMap<>(); @RequestMapping("/file_upload") public void saveFile(HttpServletRequest request, String url) throws Exception { logger.info("文件上传开始" + lineSeparator); // 1.获取从前台传过来得图片 MultipartHttpServletRequest req = (MultipartHttpServletRequest) request; MultipartFile multipartFile = req.getFile("file"); // 2.获得文件扩展名 String extOfFile = getExtOfFile(multipartFile); // 3.保存到本地 BufferedOutputStream bos = null; String filename = null; try { File dir = new File(file_save_path); if (!dir.exists()) {// 判断文件目录是否存在 dir.mkdirs(); } filename = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + (int) (Math.random() * 1000) + "." + extOfFile; bos = new BufferedOutputStream(new FileOutputStream(file_save_path + filename)); bos.write(multipartFile.getBytes()); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } public String getExtOfFile(MultipartFile multipartFile) { // 获取文件的 名称.扩展名 String oldName = multipartFile.getOriginalFilename(); String extensionName = ""; // 获取原来的扩展名 if ((oldName != null) && (oldName.length() > 0)) { int dot = oldName.lastIndexOf('.'); if ((dot > -1) && (dot < (oldName.length() - 1))) { extensionName = oldName.substring(dot+1); } } return extensionName; } }
效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“小程序采集录音并上传到后台”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新动态
20240516
20240516
- 黑帝斯哈迪斯2姊妹双刃图鉴及附魔效果一览 黑帝斯哈迪斯2全武器图文介绍
- 刘文正.2013-金典歌展2CD【南方】【WAV+CUE】
- 刘珺儿.2001-标准心上人【P.TMUSIC】【WAV+CUE】
- 谭小环.1997-自主【BMG】【WAV+CUE】
- WBG入选沙特电竞世界杯!角逐多个电竞项目
- 《小谢尔顿》最终季大结局首曝片段 成年谢米回归!
- 鸣潮凌阳声骸怎么选择 凌阳声骸搭配及词条选择攻略
- 鸣潮莫特斐声骸怎么选择 莫特斐声骸搭配及词条选择攻略
- 黑帝斯哈迪斯2月石之斧图鉴及附魔效果一览 哈迪斯斧头介绍及祝福推荐
- 甲子慧.2015-最爱【乾坤唱片】【WAV+CUE】
- 甲子慧.2007-红线情【乾坤唱片】【WAV+CUE】
- 周翠玲.2010-JZ8爵士八号【久乐实业】【WAV+CUE】
- 《家园3》公布最新预告片 高级版玩家已可抢先体验!
- 《地狱之刃2》预告对比:精心打磨三年 画质显著提升
- 《生化危机4:重制版》路径光追Mod:帧数遭暴降 画面大提升