微信小程序写一个录音机

微信小程序写一个录音机

代码:
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": "录音机"
}

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

相关推荐

  1. 程序一个录音机

    2024-06-12 06:34:03       4 阅读
  2. 程序录音机源代码

    2024-06-12 06:34:03       9 阅读
  3. 程序-语音输入(录音并播放)

    2024-06-12 06:34:03       16 阅读
  4. 程序实现一个天气预报应用程序

    2024-06-12 06:34:03       41 阅读
  5. 程序canvas手签字

    2024-06-12 06:34:03       30 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-12 06:34:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-12 06:34:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-12 06:34:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-12 06:34:03       18 阅读

热门阅读

  1. Apache Doris 基础 -- 数据表设计(分层存储)

    2024-06-12 06:34:03       6 阅读
  2. mysql安装_改密码_找回密码

    2024-06-12 06:34:03       3 阅读
  3. AWS概述

    2024-06-12 06:34:03       8 阅读
  4. C++中的单例模式

    2024-06-12 06:34:03       5 阅读
  5. WDF驱动开发-I/O请求的处理(二)

    2024-06-12 06:34:03       7 阅读
  6. 海外盲盒小程序背后的技术支撑与实现

    2024-06-12 06:34:03       4 阅读
  7. CAPL如何在底层模拟TCP Server端建立TCP连接

    2024-06-12 06:34:03       11 阅读