微信小程序录音功能实现
首先我们要做的就是长按录音功能,顺带做了个播放录音带进度条功能,效果图及代码如下:


首先我们要做的效果图就是上图这样,下面就贴代码吧。
wxml:
<view catch:longpress="handleRecordStart" catch:touchmove="handleTouchMove" catch:touchend="handleRecordStop" class="img-content" hoverClass="none" data-key="cover" data-size="1"> <image class="icon-img" mode="aspectFill" src="###"></image> </view>录音播放
<!-- 录音 --><view class="audio rel" wx:if="{{audiolist.length>0}}"> <view class="play abs" catchtap='playAudio' data-src="{{audiolist}}"> <!-- <image class="abs play-img" mode="aspectFill" src="https://retail.xiaochengxucms.com/images/12/2018/10/T8A1maB3boAB3A8Sb8yTYBs1b0BmaA.png"></image> --> <view wx:if="{{isPlayAudio}}"> <image class="abs play-img" mode="aspectFill" src='/longbing_card/resource/images/stop.png' /> </view> <view wx:else> <image class="abs play-img" mode="aspectFill" src='/longbing_card/resource/images/play.png' /> </view> </view> <view class="name abs">{{userName}}</view> <view class="flex-time"> <view style="font-size: 20rpx;color:#ccc">{{showTime1}}</view> <view class='slider'> <slider bindchange='sliderChange' activeColor='red' block-size="12" value='{{audioTime}}' /> </view> <view style="font-size: 20rpx;color:#ccc">{{showTime2}}</view> </view></view>录音播放wxss:
.play-img {
width: 62rpx !important;
height: 62rpx !important;
top: 10rpx;
left: 10rpx;
}
.audio {
width: 100%;
height: 112rpx;
background: rgba(230, 67, 64, 0.49);
border-radius: 8rpx;
margin: 10rpx 0;
}
.play {
width: 63rpx;
height: 63rpx;
top: 20rpx;
left: 40rpx;
}
.name {
font-size: 24rpx;
font-family: PingFang SC;
font-weight: 500;
line-height: 42rpx;
color: rgba(255, 255, 255, 1);
top: 14rpx;
left: 160rpx;
}
.flex-time {
width: 70%;
position: absolute;
display: flex;
align-items: center;
justify-content: flex-start;
left: 25%;
bottom: -10rpx;
}
.slider{
width: 60%;
height: 100%;
}
.slider slider{
width: 88%;
margin-left: 8%;
margin-right: 4%;
}
录音js:
// 录音
handleRecordStart: function(e) {
this.setData({
is_clock: true, //长按时应设置为true,为可发送状态
startPoint: e.touches[0], //记录触摸点的坐标信息
})
//设置录音参数
const options = {
duration: 600000,
sampleRate: 16000,
numberOfChannels: 1,
encodeBitRate: 48000,
format: ‘mp3’
}
//开始录音
recorderManager.start(options);
wx.showLoading({
icon: “none”,
title: ‘正在录音’,
})
},
handleRecordStop: function(e) {
console.log(e)
wx.hideLoading()
recorderManager.stop() //结束录音
//此时先判断是否需要发送录音
if (this.data.is_clock == true) {
var that = this
//对停止录音进行监控
recorderManager.onStop((res) => {
//对录音时长进行判断,少于2s的不进行发送,并做出提示
console.log(res)
console.log(_xx_util2.default.getUrl(“upload”, “longbing_card”))
if (res.duration < 2000) {
wx.showToast({
title: ‘录音时间太短,请长按录音’,
icon: ‘none’,
duration: 1000
})
} else {
//进行语音发送
const {
tempFilePath
} = res;
wx.showLoading({
title: ‘语音检索中’,
})
//上传录制的音频
wx.uploadFile({
url: _xx_util2.default.getUrl(“upload”, “longbing_card”),
filePath: tempFilePath,
name: ‘upfile’,
success: function(event) {
console.log(event)
var datas = JSON.parse(event.data);
console.log(datas)
if (datas.errno == 0) {
wx.hideLoading()
if (datas.data) {
console.log(that.data.audiolist)
that.setData({
audiolist: datas.data.path,
audioPath: datas.data.img,
time: res.duration
})
} else {
wx.showToast({
title: ‘上传格式错误’,
icon: ‘none’,
duration: 2000
})
}
} else {
wx.showToast({
title: datas.msg,
icon: ‘none’,
duration: 2000
})
}
}
})
}
})
}
},
handleTouchMove: function(e) {
//计算距离,当滑动的垂直距离大于25时,则取消发送语音
if (Math.abs(e.touches[e.touches.length - 1].clientY - this.data.startPoint.clientY) > 25) {
this.setData({
is_clock: false //设置为不发送语音
})
}
},
录音播放js:
var innerAudioContext = wx.createInnerAudioContext()//很重要,写在page外面,作为全局变量
data: {
isPlayAudio: false,
audioSeek: 0,
audioDuration: 0,
showTime1: ‘00:00’,
showTime2: ‘00:00’,
audioTime: 0
},
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。

