微信小程序写一个录音机

微信小程序写一个录音机

代码:

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

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

相关推荐
说私域18 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的运营策略创新研究
人工智能·小程序·开源
蓝屏的钙19 小时前
企业级微信小程序服务号授权关联机制
微信小程序
fakaifa21 小时前
【最新版】沃德云商协系统全开源+uniapp小程序
小程序·uni-app·源码下载·沃德云商协会·商协会管理系统
夏小花花2 天前
Java 日常开发笔记(小程序页面交互传参-id)
java·微信小程序·vue
说私域2 天前
基于梅特卡夫定律的开源链动2+1模式AI智能名片S2B2C商城小程序价值重构研究
人工智能·小程序·开源
2501_916013742 天前
移动端网页调试实战,跨设备兼容与触控交互问题排查全流程
android·ios·小程序·https·uni-app·iphone·webview
不如摸鱼去2 天前
uni-app 网络请求终极选型:uni.request、axios、uni-network、alova 谁才是你的真命请求库?
前端·小程序·uni-app
说私域2 天前
生产工具革命:定制开发开源AI智能名片S2B2C商城小程序重构商业生态的范式研究
人工智能·小程序·开源
hunzi_12 天前
自建纯竞拍系统小程序需准备的事项
小程序
2501_915106322 天前
TF 上架全流程实战,从构建到 TestFlight 分发
android·ios·小程序·https·uni-app·iphone·webview