微信小程序录音机源代码

html 复制代码
<!-- <button bind:tap="startTab">开始录音</button>
<button bind:tap="stopTab">结束录音</button>
<button bind:tap="playTab">播放录音</button>
<view style="margin: 0 auto;">{{time}}</view> -->
<view class="container">
  <!-- 上面部分 -->
  <view class="top">
    <view class="lyjText">录音机</view>
    <view class="times">{{h<10?'0'+ h:h}}:{{f<10?'0'+ f:f}}:{{s<10?'0'+ s:s}}</view>
  </view>
  <!-- 下面部分 -->
  <view class="player">
  <!-- 暂停 -->
    <view bind:tap="pauseTab" class="playerBox sjx"></view>
    <!-- 开始录音 -->
    <view class="playerBox1">
      <view class="litterBox" bind:tap="startTab">
      </view>
    </view>
    <!-- 结束录音 -->
    <view class="playerBox" bind:tap="stopTab">
      <view class="zfx"></view>
    </view>
  </view>
  <button type="primary" bind:tap="playTab">播放</button>
</view>
css 复制代码
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  height: 100vh;
}
.player {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.playerBox1 {
  width: 200rpx;
  height: 200rpx;
  background-color: red;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 50rpx;
}
.litterBox {
  width: 90rpx;
  height: 90rpx;
  background-color: white;
  border-radius: 50%;
}
.playerBox {
  width: 140rpx;
  height: 140rpx;
  background-color: rgb(239,239,239);
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.zfx {
  width: 40rpx;
  height: 40rpx;
  background-color: rgb(174,174,174);
}
.sjx::after {
  content: "";
  display: block;
  border: 30rpx solid transparent;
  border-left-color: rgb(174,174,174);
  position: relative;
  left: 15rpx;
}
.lyjText {
  font-size: 50rpx;
}
.times {
  font-size: 120rpx;
}
.top {
  display: flex;
  flex-direction: column;
  align-items: center;
}
javascript 复制代码
// 全局录音管理器
let recorder = wx.getRecorderManager()
// 全局文件管理
// let fileSytems = wx.getFileSystemManager()
let audio = wx.createInnerAudioContext()
let timer = null
let _this
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 小时
    h: 0,
    // 分钟
    f: 0,
    // 秒
    s: 0,
    file: '',
    // 是否在播放
    isPlay: false
  },
  onLoad() {
    _this = this
  },
  pauseTab() {
    if (this.data.isPlay === true) {
      this.setData({
        isPlay: false
      })
      recorder.pause()
      clearInterval(timer)
      timer = null
    } else if (this.data.isPlay === false) {
      this.startTab()
    }
  },
  startTab() {
    this.setData({
      isPlay: true
    })
    if (!timer) {
      timer = setInterval(() => {
        this.setData({
          s: this.data.s >= 59 ? 0 : this.data.s + 1,
          f: this.data.s >= 59 ? this.data.f + 1 : this.data.f,
          h: this.data.f >= 59 ? this.data.h + 1 : this.data.h,
        })
      }, 1000)
    }

    recorder.start()
  },
  stopTab() {
    this.setData({
      isPlay: false
    })
    clearInterval(timer)
    timer = null
    this.setData({
      f: 0,
      h: 0,
      s: 0
    })
    recorder.stop()
    // console.log(1);
    recorder.onStop(res => {
      console.log(1);
      const {
        tempFilePath
      } = res
      // console.log(res);
      this.data.file = tempFilePath
      console.log(this.data.file);
    })
  },
  playTab() {
    audio.src = this.data.file
    audio.play()
  },
})
相关推荐
Micro麦可乐14 分钟前
JavaScript异步编程 Async/Await 使用详解:从原理到最佳实践
开发语言·javascript·ecmascript·async/await·异步编程
程序员阿龙4 小时前
基于微信小程序的高校校园微活动管理系统设计与实现(源码+定制+开发)高校微信小程序校园活动发布与互动平台开发 面向大学生群体的校园活动移动平台设计与实现
微信小程序·小程序·活动发布与管理·高校校园活动平台·社团组织管理系统·学生报名签到小程序·移动端高校活动系统
BillKu5 小时前
Vue3 + Element Plus 中修改表格当前选中行的颜色
前端·vue.js·elementui
BillKu5 小时前
Axios中POST、PUT、PATCH用法区别
前端·vue.js
好奇的菜鸟6 小时前
掌握 npm 核心操作:从安装到管理依赖的完整指南
前端·npm·node.js
广西千灵通网络科技有限公司7 小时前
基于Java的话剧购票小程序【附源码】
java·小程序·apache
STUPID MAN7 小时前
arcgis js统计FeatureLayer的椭球面积、平面面积
javascript·arcgis·椭球面积·平面面积
肥肠可耐的西西公主8 小时前
前端(小程序)学习笔记(CLASS 2):WXML模板语法与WXSS模板样式
前端·学习·小程序
佛系菜狗8 小时前
【菜狗work前端】小程序加if判断时不及时刷新 vs Web
微信小程序·uniapp·web
JAVA学习通8 小时前
JavaScript网页开发设计(轮播图)
javascript