微信小程序写一个录音机

微信小程序写一个录音机

代码:

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

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

相关推荐
码视野3 小时前
完全开源-支持二开-可做毕业论文-家政服务预约小程序
小程序
码视野3 小时前
全开源-健身运动预约小程序 — 从需求到原型的全栈实践
小程序
游戏开发爱好者84 小时前
深入理解iOSTime Profiler:提升iOS应用性能的关键工具
android·ios·小程序·https·uni-app·iphone·webview
tianxiaxue14 小时前
微信小程序如何跟企微互通
微信小程序·小程序·企业微信
Greg_Zhong4 小时前
微信小程序中canvas绘制面积图,解决手机和模拟器都能渲染不溢出问题
微信小程序·小程序canvas绘制面积图
小小王app小程序开发5 小时前
潮玩抽赏盲盒小程序玩法分析:2026 潮玩风口,技术合规双驱动
小程序
小小王app小程序开发5 小时前
AI 智能体小程序玩法分析:2026 千亿 AI 风口,冠品科技赋能低门槛落地
人工智能·科技·小程序
头发还在的女程序员18 小时前
家政SaaS平台开源:从供应商入驻到分账结算,源码如何设计?
小程序·开源
焦糖玛奇朵婷21 小时前
解锁扭蛋机小程序的五大优势
java·大数据·服务器·前端·小程序
web前端神器1 天前
记录uniapp小程序的报错
小程序·uni-app·apache