微信小程序写一个录音机

微信小程序写一个录音机

代码:

wxml:

html 复制代码
<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:

css 复制代码
.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:

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:

json 复制代码
{
  "usingComponents": {},
  "navigationBarTitleText": "录音机"
}

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

相关推荐
hello kitty w4 小时前
3.小程序方法的封装
javascript·小程序
蓝色心灵-海4 小时前
数字化家谱管理实践:基于微信小程序的家族信息整理方案(宗源智谱)族谱,家谱管理
小程序
WX:ywyy67984 小时前
从0到1:付费短剧小程序全栈定制开发实战与避坑指南
微信·小程序·短剧小程序开发·短剧系统开发·短剧app开发·短剧平台开发·短剧平台搭建
笨笨狗吞噬者5 小时前
【uniapp】小程序端解决分包的uni_modules打包后产物进入主包中的问题
前端·微信小程序·uni-app
天空属于哈夫克37 小时前
Java 开发|企微外部群主动发送小程序消息实战
开发语言·python·小程序·自动化·企业微信·rpa
焦糖玛奇朵婷7 小时前
做盲盒小程序,如何少走弯路?
数据库·程序人生·小程序·开源软件·软件需求
玛雅牛牛8 小时前
商城小程序实用度多家对比+新手友好款
大数据·小程序
CHU7290358 小时前
货运物流APP前端交互创新:以用户为中心重构运输服务全链路
java·前端·小程序·重构
幽络源小助理8 小时前
小程序源码分享:模拟人生游戏源码
微信小程序·游戏源码