小程序实现录音上传功能

 更新时间:2019年11月22日 16:05:15   作者:钦晨  
这篇文章主要为大家详细介绍了小程序实现录音上传功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

首先我们可以先看一下微信小程序的API

这里有关于小程序录音的一些基本配置

index.wxml:

<view class='progress_box' bindtap='openRecording' style="display:{{openRecordingdis}}">
    <view class="progress_bgs">
     <view class="progress_bg">
      <image class="progress_img" src='../../../images/SubjectInformation/luyin.png'></image>
     </view>
   </view>
</view>

index.wxss:

.topicRecording {
 float: left;
 width: 40%;
 height: 100%;
 position: relative;
}
 
 
.progress_box {
 width: 130rpx;
 height: 130rpx;
 margin-left: -65rpx;
 position: absolute;
 bottom: 0;
 left: 50%;
 display: flex;
 align-items: center;
 justify-content: center;
 background: #ccc;
 border-radius: 50%;
}
 
.progress_bgs {
 width: 114rpx;
 height: 114rpx;
 background: #fff;
 border-radius: 50%;
 margin: 9rpx;
}
 
 
.progress_bg {
 width: 106rpx;
 height: 106rpx;
 margin: 5rpx;
 position: absolute;
 background: #444;
 border-radius: 50%;
}
 
 
 
.progress_img {
 width: 82rpx;
 height: 82rpx;
 border-radius: 50%;
 margin: 12rpx;
}

index.js:

Page({
 data: {
  openRecordingdis: "block",//录音图片的不同
  shutRecordingdis: "none",//录音图片的不同
  recordingTimeqwe:0,//录音计时
  setInter:""//录音名称
 },
 
  //录音计时器
 recordingTimer:function(){
  var that = this;
  //将计时器赋值给setInter
  that.data.setInter = setInterval(
   function () {
    var time = that.data.recordingTimeqwe + 1;
    that.setData({
     recordingTimeqwe: time
    })
   }
   , 1000); 
 },
 
 
 //开始录音
 openRecording: function() {
  var that = this;
  wx.getSystemInfo({
   success: function(res) {
    that.setData({
     shutRecordingdis: "block",
     openRecordingdis: "none"
    })
   }
  })
  const options = {
   duration: 60000, //指定录音的时长,单位 ms,最大为10分钟(600000),默认为1分钟(60000)
   sampleRate: 16000, //采样率
   numberOfChannels: 1, //录音通道数
   encodeBitRate: 96000, //编码码率
   format: 'mp3', //音频格式,有效值 aac/mp3
   frameSize: 50, //指定帧大小,单位 KB
  }
  //开始录音计时  
  that.recordingTimer();
  //开始录音
  recorderManager.start(options);
  recorderManager.onStart(() => {
   console.log('。。。开始录音。。。')
  });
  //错误回调
  recorderManager.onError((res) => {
   console.log(res);
  })
 },
 
 //结束录音
 shutRecording: function() {
  var that = this;
  wx.getSystemInfo({
   success: function(res) {
    that.setData({
     shutRecordingdis: "none",
     openRecordingdis: "block"
    })
   }
  })
  recorderManager.stop();
  recorderManager.onStop((res) => {
   console.log('。。停止录音。。', res.tempFilePath)
   const {tempFilePath} = res;
   //结束录音计时 
   clearInterval(that.data.setInter);
   //上传录音
   wx.uploadFile({
    url: appURL + '/wx_SubjectInformation/wx_SubjectRecordKeeping.do',//这是你自己后台的连接
    filePath: tempFilePath,
    name:"file",//后台要绑定的名称
    header: {
     "Content-Type": "multipart/form-data"
    },
    //参数绑定
    formData:{
     recordingtime: that.data.recordingTimeqwe,
     topicid: that.data.topicid,
     userid:1,
     praisepoints:0
    },
    success:function(ress){
     console.log(res);
     wx.showToast({
      title: '保存完成',
      icon:'success',
      duration:2000
     })
    },
    fail: function(ress){
     console.log("。。录音保存失败。。");
    }
   })
  })
 },
 
 //录音播放
 recordingAndPlaying: function(eve) {
  wx.playBackgroundAudio({
   //播放地址
   dataUrl: '' + eve.currentTarget.dataset.gid + ''
  })
 },
 
})

上传服务

@RequestMapping(value = "/wx_SubjectRecordKeeping", produces = "application/json")
 @ResponseBody
 public Object wx_SubjectRecordKeeping(HttpServletRequest request,
  @RequestParam("file") MultipartFile files, String recordingtime,
  int topicid,int userid,int praisepoints) {
 // 构建上传目录路径
 // request.getServletContext().getRealPath("/upload");
 String uploadPath = 你自己保存音频的URL;
 // 如果目录不存在就创建
 File uploadDir = new File(uploadPath);
 if (!uploadDir.exists()) {
  uploadDir.mkdir();
 }
 // 获取文件的 名称.扩展名
 String oldName = files.getOriginalFilename();
 String extensionName = "";
 // 获取原来的扩展名
 if ((oldName != null) && (oldName.length() > 0)) {
  int dot = oldName.lastIndexOf('.');
  if ((dot > -1) && (dot < (oldName.length() - 1))) {
  extensionName = oldName.substring(dot);
  }
 }
 // 构建文件名称
 String fileName = System.currentTimeMillis() + "_" + System.nanoTime()
  + extensionName;
 // 获取
 String[] fileType = { ".CD", ".WAVE", ".AIFF", ".AU", ".MPEG", ".MP3",
  ".MPEG-4", ".MIDI", ".WMA", ".RealAudio", ".VQF", ".OggVorbis",
  ".AMR" };
 List<String> fileTyepLists = Arrays.asList(fileType);
 int fileTypeOnCount = 0;
 for (String fileTyepListss : fileTyepLists) {
  if (fileTyepListss.equalsIgnoreCase(extensionName)) {
  // -----如果是音频文件的话
  // 构建文件路径
  String filePath = uploadPath + File.separator + fileName;
  // 保存文件
  try {
   FileUtils.writeByteArrayToFile(new File(filePath),
    files.getBytes());
  } catch (Exception e) {
   e.printStackTrace();
  }
  } else {
  fileTypeOnCount++;
  }
 }
 if (fileTypeOnCount == fileTyepLists.size()) {
  // 不是音频文件
  return false;
 }
 return false;
 }

效果图

点击开始录音、录完后点击结束录音

录音成功后的返回

录制的音频文件

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

相关文章

  • css结合js制作下拉菜单示例代码

    css结合js制作下拉菜单示例代码

    这篇文章主要介绍了css结合js制作下拉菜单的具体实现,需要的朋友可以参考下
    2014-02-02
  • 微信小程序缓存过期时间的使用详情

    微信小程序缓存过期时间的使用详情

    这篇文章主要介绍了微信小程序缓存过期时间的使用详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-05-05
  • JavaScript结合canva实现简单的绘图工具

    JavaScript结合canva实现简单的绘图工具

    这篇文章主要给大家介绍了如何使用JavaScript和HTML的Canvas标签创建一个简单的图表工具,文中通过代码介绍的非常详细,对大家的学习或者工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2024-12-12
  • 纯css实现窗户玻璃雨滴逼真效果

    纯css实现窗户玻璃雨滴逼真效果

    css实现窗户上水珠效果,效果特别逼真,窗外的雨淅淅沥沥飘打在玻璃上,看起来很像模糊的窗外,斜滴的雨露,接下来给大家一起来用CSS技术实现这样一幅画面,下面给大家分享使用纯css实现窗户玻璃雨滴逼真效果,感兴趣的朋友快乐围观吧
    2015-08-08
  • JS中的算法与数据结构之集合(Set)实例详解

    JS中的算法与数据结构之集合(Set)实例详解

    这篇文章主要介绍了JS中的算法与数据结构之集合(Set),结合实例形式详细分析了javascript中集合的概念、原理、定义及相关操作技巧,需要的朋友可以参考下
    2019-08-08
  • 微信小程序音频录制波纹循环动画

    微信小程序音频录制波纹循环动画

    这篇文章主要为大家详细介绍了微信小程序音频录制波纹循环动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • js获取数组的最后一个元素

    js获取数组的最后一个元素

    这篇文章主要介绍了javascript获取数组的最后一个元素,需要的朋友可以参考下
    2015-04-04
  • 跟我学Node.js(四)---Node.js的模块载入方式与机制

    跟我学Node.js(四)---Node.js的模块载入方式与机制

    Node.js中模块可以通过文件路径或名字获取模块的引用。模块的引用会映射到一个js文件路径,除非它是一个Node内置模块。Node的内置模块公开了一些常用的API给开发者,并且它们在Node进程开始的时候就预加载了。
    2014-06-06
  • 详解ES6中的Map与Set集合

    详解ES6中的Map与Set集合

    这篇文章主要介绍了详解ES6中的Map与Set集合,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • tsconfig.json配置详解

    tsconfig.json配置详解

    这篇文章主要介绍了tsconfig.json配置详解,tsconfig.json文件中指定了用来编译这个项目的根文件和编译选项,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-05-05

最新评论