微信小程序实现录音
作者:示羊online 发布时间:2024-04-22 13:25:41
标签:微信小程序,录音
本文实例为大家分享了微信小程序实现录音的具体代码,供大家参考,具体内容如下
为录音
录音中
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
}
播放音频:微信小程序实现播放音频
来源:https://blog.csdn.net/onion_line/article/details/86529049


猜你喜欢
- Mootools 1.2手风琴(Accordion)教程原文地址:30 Days of Mootools 1.2 Tutoria
- 本文实例讲述了python装饰器常见使用方法。分享给大家供大家参考,具体如下:python 的装饰器,可以用来实现,类似spring AOP
- 本文实例讲述了Python实现定制自动化业务流量报表周报功能。分享给大家供大家参考,具体如下:一 点睛本次实践通过定制网站5个频道的流量报表
- python中在实现一元线性回归时会使用最小二乘法,那你知道最小二乘法是什么吗。其实最小二乘法为分类回归算法的基础,从求解线性透视图中的消失
- import osimport codecsfilenames=os.listdir(os.getcwd())out=file("
- 以下的文章主要是介绍SQL Server数据转换服务的4妙用之执行一些自动化的操作。在SQL Server数据库的实际操作管理中,数据库管理
- <% SoFia_DbName="jiaoyou&qu
- 一、数据集下载加州高速公路PEMS数据集这里绘制PEMS04中的交通流量数据。该数据集中包含旧金山2018年1月1日至2月28日的29条道路
- 作为抛砖引玉,用python3实现百度云语音解析,首先需要模拟Post请求把音频压缩文件丢给百度解析。但是遇到一个问题客户端怎麽丢数据都是返
- 这篇文章主要介绍了Python globals()和locals()对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的
- 本人机器环境:Windows 2008 R2MySQL 5.6以“Window下忘记Mysql的root密码”百度,找到一大堆解决方案。大多
- 如下所示:# 创建一个空的 DataFramedf_empty = pd.DataFrame()#或者df_empty = pd.DataF
- 一、字符串类型1)字符串是字符的序列表示,根据字符的内容分为单行字符串和多行字符串。2)单行字符串可以由一对单引号(’)
- 本文实例为大家分享了python合并同类型excel表格的具体代码,供大家参考,具体内容如下python脚本如下,验证有效。#!/usr/b
- 本篇主要讲述如何基于NUXT的validate方法实现表单的验证。将验证方法封装后,使用的时候只需像:rules="filter_
- 本文实例讲述了JS小游戏的象棋暗棋源码,分享给大家供大家参考。具体如下:游戏运行后如下图所示:Javascript 部分:/** chine
- 从MySQL 5.0 开始,支持了一个全新的SQL句法:PREPARE stmt_name FROM preparable_stmt;EXE
- 参数数量及其作用该函数共有两个参数,分别是key和scope。def get_collection(key, scope=None) Wra
- java后台相关问题一,程序包lombok不存在通常报这个错误,是因为,我们的开发者工具idea没有安装lombok库导致的解决办法如下:提
- 这篇文章主要介绍了基于Python批量生成指定尺寸缩略图代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值