微信小程序实现录音

 更新时间:2022年06月24日 14:06:31   作者:示羊online  
这篇文章主要为大家详细介绍了微信小程序实现录音,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了微信小程序实现录音的具体代码,供大家参考,具体内容如下

为录音

录音中

wxml:

<!-- 开始录音 -->
<image src="/img/add_voice.png" class="add-voice" wx:if="{{record == 0 }}" bindtap="startRecord"></image>
 
<!-- 录音中 -->
<block wx:if="{{record == 1}}">
<view class='audio'>
  <view class='laudio_btn' bindtap='remove'>
    <image src='/img/btn_close2.png' style='width:26rpx;height:25rpx;'></image>
  </view>
  <text class='laudio_text1'>|</text>
  <text class='laudio_text2'>{{formatedRecordTime}}</text>
  <view class='laudio_pro'>
    <image src='/img/btn_play2.png' style='width:100%;height:100%' catchtap='keep' wx:if="{{keep}}"></image>
    <image src='/img/btn_play3.png' style='width:100%;height:100%' catchtap='pause' wx:else></image>
  </view>
  <text class='audio_text' catchtap='stopRecord'>完成</text>
</view>
</block>

wxss:

.add-voice {
  width: 158rpx;
  height: 158rpx;
}
 
.audio {
  display: flex;
  position: relative;
  height: 140rpx;
  line-height: 140rpx;
  background: #f7f7f7;
  width: 96%;
}
 
/* 传语音 */
.audio{
  display: flex;
  position: relative;
  height:140rpx;
  line-height: 140rpx;
  background: #f7f7f7;
  width: 96%;
}
/* 录语音 */
.laudio_btn{
  /* width:26rpx;
  height:26rpx; */
  margin-left:42rpx
}
.audio_img{
  position: absolute;
  /* top:-15rpx; */
  /* right:-15rpx; */
  right: 0;
  width:30rpx;
  height:30rpx;
}
.audio_btn{
  width:88rpx;
  height:88rpx;
  margin-top:28rpx;
   margin-left:16rpx
}
.audio_pro{
  margin-top:36rpx;
  margin-left:20rpx;
  width:300rpx;
}
.audio_text{
  font-weight: bold;
  margin-left:50rpx;
  color:#90BED5;
  font-size: 10pt
}
.laudio_text1{
  font-size: 26rpx;
  margin-left:20rpx;
  color: #90BED5
}
.laudio_text2{
  font-weight: bold;
  font-size: 26rpx;
  margin-left:20rpx;
  color: #F8624E;
  width: 381rpx;
}
.laudio_pro{
  width:88rpx;
  height:88rpx;
  margin-top:28rpx;
}

js:

var util = require('../../utils/count.js') //计算时分秒函数
let radio = wx.getRecorderManager(); //创建录音
const app = getApp()
 
var recordTimeInterval; //录音 时分秒 '00:00'
 
Page({
  data: {
    record : 0, //未录音
    formatedRecordTime: '00:00',
    keep: true,
    recordTime: 0
  },
  //开始录音
  startRecord: function () {
    var that = this
    this.setData({ record: 1 })
    recordTimeInterval = setInterval(function () { //计算时分秒
      var recordTime = that.data.recordTime += 1
      that.setData({
        formatedRecordTime: util.formatTime(that.data.recordTime),
        recordTime: recordTime
      })
    }, 1000)
    const options = {
      duration: 30000, //录音
      sampleRate: 44100,
      numberOfChannels: 1,
      encodeBitRate: 192000,
      format: 'mp3'
    }
    radio.start(options); //开始录音
    radio.onStart((res => {
      console.log('监听录音', res)
    }));//监听录音事件
  },
  //暂停录音
  keep() {
    radio.resume();
    this.setData({ keep: false })
    clearInterval(recordTimeInterval);
  },
  //继续录音
  pause() {
    var that = this
    this.setData({ keep: true, })
    recordTimeInterval = setInterval(function () { //计算时分秒
      var recordTime = that.data.recordTime += 1
      that.setData({
        formatedRecordTime: util.formatTime(that.data.recordTime),
        recordTime: recordTime
      })
    }, 1000)
  },
  //结束录音
  stopRecord: function () {
    console.log('录音结束了')
    var that = this;
    clearInterval(recordTimeInterval);
    radio.stop(); //录音结束
    radio.onStop((res) => { //录音结束
      // that.stopRecord
      console.log('录音结束', res);
      this.setData({
        record: res.tempFilePath,
        musicUrl: res.tempFilePath, //录音音频
        duration: res.duration, //音频时长时间戳
        record: true,
      })
    })
  },
  //取消录音
  remove() {
    radio.stop(); //结束录音
    this.setData({ record: 0, recordTime: 0, play: false })
    clearInterval(recordTimeInterval);
  },
})

utils/count.js

function formatTime(time) {
  if (typeof time !== 'number' || time < 0) {
    return time
  }
 
  var hour = parseInt(time / 3600)
  time = time % 3600
  var minute = parseInt(time / 60)
  time = time % 60
  var second = time
 
  return ([hour, minute, second]).map(function (n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }).join(':')
}
 
function formatLocation(longitude, latitude) {
  if (typeof longitude === 'string' && typeof latitude === 'string') {
    longitude = parseFloat(longitude)
    latitude = parseFloat(latitude)
  }
 
  longitude = longitude.toFixed(2)
  latitude = latitude.toFixed(2)
 
  return {
    longitude: longitude.toString().split('.'),
    latitude: latitude.toString().split('.')
  }
}
 
module.exports = {
  formatTime: formatTime,
  formatLocation: formatLocation
}

播放音频:微信小程序实现播放音频

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • javascript之弹出窗口居中的代码

    javascript之弹出窗口居中的代码

    有时候我们在网页制作过程中需要弹出窗口,一般情况下,我们更希望窗口可以居中显示
    2007-08-08
  • 微信小程序实现评价功能

    微信小程序实现评价功能

    这篇文章主要为大家详细介绍了微信小程序实现评价功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • js实现的简单图片浮动效果完整实例

    js实现的简单图片浮动效果完整实例

    这篇文章主要介绍了js实现的简单图片浮动效果,以完整实例形式分析了javascript实现图片定时漂浮移动功能的方法,涉及javascript定时函数与数值动态运算相关技巧,需要的朋友可以参考下
    2016-05-05
  • JavaScript实现文本相似度对比

    JavaScript实现文本相似度对比

    这篇文章主要介绍了JavaScript实现文本相似度对比,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-06-06
  • JavaScript数据类型的转换详解

    JavaScript数据类型的转换详解

    JavaScript是一种动态类型语言,变量没有类型限制,可以随时赋予任意值。本文就来和大家聊聊JavaScript中的数据类型转换,感兴趣的小伙伴可以了解一下
    2022-12-12
  • 微信小程序购物商城系统开发系列-工具篇的介绍

    微信小程序购物商城系统开发系列-工具篇的介绍

    这篇文章主要介绍了微信小程序购物商城系统开发系列-工具篇的介绍,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2016-11-11
  • JS实现容器模块左右拖动效果

    JS实现容器模块左右拖动效果

    这篇文章主要为大家详细介绍了JS实现容器模块左右拖动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • JS结合WebSocket实现实时双向通信

    JS结合WebSocket实现实时双向通信

    WebSocket 是一种在 Web 应用程序中实现实时、双向通信的协议,在本文中,我们将深入介绍 WebSocket 的原理、用法以及一些实际应用场景,x需要的可以参考下
    2023-11-11
  • js手机号批量滚动抽奖实现代码

    js手机号批量滚动抽奖实现代码

    这篇文章主要为大家详细介绍了js手机号批量滚动抽奖实现代码,s适用于年会等活动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • JS 实现Json查询的方法实例

    JS 实现Json查询的方法实例

    曾经看过一个大牛写的实现Json的一个模板类,今天突然没事就来自己试着写写。还好,一些东西还记得,思路还算清晰。直接上代码了
    2013-04-04

最新评论