• 微信小程序写一个录音机


    微信小程序写一个录音机

    代码:
    wxml:

    <view class="container">
      <view class="duration">{{duration}}view>
      <button bindtap="startRecord" class="btn">开始录音button>
      <button bindtap="stopRecord" class="btn">停止录音button>
      <button bindtap="playRecord" class="btn">播放录音button>
    view>
    <audio id="audio" src="{{recordPath}}" controls>audio>
    

    wxss:

    .container {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      height: 84vh;
    }
    
    .btn {
      margin: 10px;
      padding: 10px 20px;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 5px;
    }
    
    .duration {
      margin-top: -50px;
      font-size: 24px;
    }
    
    

    js:

    let timer = null;
    Page({
      data: {
        isRecording: false,
        recordPath: '',
        duration: '00:00'
      },
      startRecord: function() {
        const recorderManager = wx.getRecorderManager();
        this.setData({
          isRecording: true,
          duration: '00:00'
        });
        let startTime = new Date().getTime();
        timer = setInterval(() => {
          let currentTime = new Date().getTime();
          let diff = currentTime - startTime;
        
          let m = Math.floor(diff / 60000 % 60);
          let s = Math.floor(diff / 1000 % 60);
          this.setData({
            duration: `${this.formatTime(m)}:${this.formatTime(s)}`
          });
        }, 1000);
        recorderManager.start({
          format: 'mp3'
        });
        recorderManager.onStart(() => {
          console.log('recorder start');
        });
        recorderManager.onStop((res) => {
          console.log('recorder stop', res);
          this.setData({
            recordPath: res.tempFilePath,
            isRecording: false
          });
        });
      },
      stopRecord: function() {
        const recorderManager = wx.getRecorderManager();
        clearInterval(timer);
        recorderManager.stop();
      },
      playRecord: function() {
        this.setData({
          // isRecording: true,
          duration: '录音播放'
        });
        const audioCtx = wx.createInnerAudioContext();
        audioCtx.src = this.data.recordPath;
        audioCtx.play();
      },
      formatTime: function(time) {
        return time < 10 ? `0${time}` : time;
      }
    });
    
    

    json:

    {
      "usingComponents": {},
      "navigationBarTitleText": "录音机"
    }
    

    以上就是实现一个录音机的小程序!

  • 相关阅读:
    YbtOJ「数据结构」第4章 线段树
    如果线性变换可以模仿
    debian 12.5 使用官方源、清华源等,无法安装wget、mysql,如何解决?(操作系统-linux)
    无线产品欧盟CE认证RED指令测试要求解读
    从零入门 Vite 与 Webpack 对比
    LVDS转换芯片,GM8284C数据手册!!!!
    一文介绍使用 JIT 认证后实时同步用户更加优雅
    (零)如何做机器视觉项目
    python经典百题之逐字写文件
    9.18日学习记录
  • 原文地址:https://blog.csdn.net/xj1216wt/article/details/139595187