微信小程序实现录音上传

微信小程序实现录音上传

准备

1.微信开发者工具
2.Api文档

开始

打开微信开发者工具
项目->新建项目
选择小程序
在这里插入图片描述
appid是在微信公众平台申请的。
链接
新建完成后,项目自带有模板。结构如图。
在这里插入图片描述
audio是我新建的。其余都是项目生成的。
wxml文件相当于html,wxss相当于css文件,json里可以方配置或者临时数据之类的。
在audio.wxml中添加开始录制按钮。

<!--pages/audio/audio.wxml-->
<view class="container">
  <button bindtap='record'>开始录音</button>
  <button bindtap='upload'>开始上传</button>  
  <text>pages/audio/audio.wxml</text>
</view>

对应的js文件下

Page({
  data: {
    tmpfile:'',
    time:''
  },
  record: function(e){
    var that = this;
    const starttime = Date.now();
    this.recorderManager = wx.getRecorderManager();
    this.recorderManager.onError(function(res){
      console.log(res)//错误信息自己判断
    })
    this.recorderManager.onStop(function(res){
      that.setData({
        tmpfile: res.tempFilePath,
        time: Date.now() - starttime,
      })
    })
    this.recorderManager.start({
      format:"mp3"
    })
    setTimeout(function(){
      that.recorderManager.stop();
    },10000);
  },
  stopRecord:function(e){
    console.log("stop")
    this.recorderManager.stop();
  },
  upload:function(){
    console.log("开始上传",this.data.time)
    wx.uploadFile({
      filePath: this.data.tmpfile, //录音文件
      name: 'name',
      url: '后台接口',
      format:{
        audio_ms: this.data.time,//时间长
      }
    })
  }
})

注:wx.stopRecord(Object object) 和 wx.startRecord(Object object) 从基础库 1.6.0 开始,接口停止维护。

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值