国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 幫助中心 > 正文

小程序采集錄音并上傳到后臺

2024-07-09 22:41:47
字體:
來源:轉載
供稿:網友

本文實例為大家分享了小程序錄音上傳的具體代碼,供大家參考,具體內容如下

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.jsconst 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; }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绍兴县| 紫云| 论坛| 察哈| 车致| 通化市| 邓州市| 宁陕县| 长汀县| 沁源县| 唐河县| 潼南县| 阜新市| 临沭县| 淄博市| 五寨县| 崇信县| 三穗县| 榆林市| 黑水县| 光泽县| 会理县| 冕宁县| 新乡市| 察隅县| 襄樊市| 庐江县| 正宁县| 嘉义县| 白城市| 文登市| 浦县| 仁怀市| 米脂县| 永福县| 会昌县| 元朗区| 莱州市| 兴国县| 珠海市| 云阳县|