微信小程序写一个录音机

微信小程序写一个录音机

代码:

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

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

相关推荐
StarChainTech1 小时前
一站式租车平台革新:从信用免押到全流程可视化管理的技术实践
大数据·人工智能·微信小程序·小程序·软件需求
CHU7290353 小时前
一番赏盲盒抽卡机小程序:探索惊喜与互动的多元功能体验
小程序
换日线°4 小时前
微信小程序对接位置服务(腾讯、高德)完成路径规划
前端·微信小程序·vue
2501_915918414 小时前
Wireshark、Fiddler、Charles抓包工具详细使用指南
android·ios·小程序·https·uni-app·iphone·webview
苏苏哇哈哈4 小时前
微信小程序实现仿腾讯视频小程序首页圆角扩散轮播组件
微信小程序·小程序·轮播图
code袁5 小时前
基于微信小程序的宿舍维修小程序的设计与实现
微信小程序·小程序·毕业设计·springboot·notepad++·宿舍维修小程序
说私域5 小时前
AI智能客服S2B2C商城小程序在客户服务场景中的应用与价值——以顾客反馈处理为例
人工智能·小程序·流量运营·私域运营
黄菊华老师13 小时前
微信小程序毕业设计:渔具商城小程序毕设源码作品和开题报告
微信小程序·小程序·课程设计·渔具商城小程序
攻城狮7号13 小时前
不懂代码也能造?TRAE+GLM-4.6 手把手教你搭心理咨询智能客服小程序
python·小程序·uni-app·vue·trae·glm我的编程搭子·glm-4.6
QQ40220549616 小时前
基于微信小程序的大学班级管理系统的设(作业 考勤 请假 投票)
微信小程序·小程序·毕业设计·notepad++