栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

小程序采集录音并上传到后台

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

小程序采集录音并上传到后台

本文实例为大家分享了小程序录音上传的具体代码,供大家参考,具体内容如下

demo.wxml


 


 


 


 

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
    })
  }

  
  , 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/";

 
 protected Map dataMap = new HashMap<>();
 
 
 protected Map 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;
 }
}

效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/72308.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号