标题五个字

<view class="container">

 <button type="primary"  bindtap="startRecord">录音</button>

 <button type=""  bindtap="stopRecord">停止</button>

 <button type="warn"  bindtap="uploadFile">上传</button>

</view>

// index.js

Page({

  data: {

    recorderManager: null, // 录音管理器

    tempFilePath: '', // 录音文件临时路径

    uploadProgress: 0, // 上传进度

    uploadFilePath: '', // 上传后的文件路径

  },

  

  // 开始录音

  startRecord: function() {

    wx.authorize({

      scope: 'scope.record',

      success: (res) => {

        console.log('用户授权成功');

      },

      fail: (res) => {

        console.log('用户拒绝授权');

      }

    })

    

    const recorderManager = wx.getRecorderManager();

    recorderManager.onStart(() => {

      console.log('录音开始');

    });

    recorderManager.start({

      duration: 60000, // 录音时长60s

      format: 'mp3', // 录音格式

    });

    this.setData({

      recorderManager: recorderManager,

    });

    recorderManager.onError(function(res){

      console.error(res)

    })

  },

  // 停止录音

  stopRecord: function() {

    const recorderManager = this.data.recorderManager;

    recorderManager.onStop((res) => {

      console.log('录音停止');

      this.setData({

        tempFilePath: res.tempFilePath,

      });

    });

    recorderManager.stop();

  },

  // 上传录音文件

  uploadFile: function() {

    wx.uploadFile({

      url: 'http://localhost/upload', // 上传地址

      filePath: this.data.tempFilePath, // 要上传的文件路径

      name: 'file', // 上传参数名

      success: (res) => {

        console.log('上传成功');

        console.log(res.data);

        const data = JSON.parse(res.data);

        this.setData({

          uploadFilePath: data.filePath,

        });

      },

      fail: (res) => {

        console.log('上传失败');

        console.log(res);

      },

      complete: () => {

        console.log('上传完成');

      },

      // 显示上传进度

      onProgressUpdate: (res) => {

        console.log('上传进度', res.progress);

        this.setData({

          uploadProgress: res.progress,

        });

      },

    });

  },

});


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值