<!-- index.wxml -->
<view class="container">
<button bindtap="startRecording" disabled="{{isRecording}}">开始录音</button>
<button bindtap="stopRecording" disabled="{{!isRecording}}">停止录音</button>
<view wx:if="{{tempFilePath}}">
<audio src="{{tempFilePath}}" controls></audio>
</view>
</view>
/**index.wxss**/
page {
height: 100vh;
display: flex;
flex-direction: column;
}
.scrollarea {
flex: 1;
overflow-y: hidden;
}
.userinfo {
display: flex;
flex-direction: column;
align-items: center;
color: #aaa;
width: 80%;
}
.userinfo-avatar {
overflow: hidden;
width: 128rpx;
height: 128rpx;
margin: 20rpx;
border-radius: 50%;
}
.usermotto {
margin-top: 200px;
}
.avatar-wrapper {
padding: 0;
width: 56px !important;
border-radius: 8px;
margin-top: 40px;
margin-bottom: 40px;
}
.avatar {
display: block;
width: 56px;
height: 56px;
}
.nickname-wrapper {
display: flex;
width: 100%;
padding: 16px;
box-sizing: border-box;
border-top: .5px solid rgba(0, 0, 0, 0.1);
border-bottom: .5px solid rgba(0, 0, 0, 0.1);
color: black;
}
.nickname-label {
width: 105px;
}
.nickname-input {
flex: 1;
}
// index.js
const recorderManager = wx.getRecorderManager();
Page({
data: {
isRecording: false,
tempFilePath: ''
},
startRecording: function() {
const options = {
duration: 60000, // 录音最长时间,单位为毫秒,这里设置为1分钟
sampleRate: 44100, // 采样率,44.1kHz是目前的标准
numberOfChannels: 1, // 录音通道数,这里设置为单声道
encodeBitRate: 192000, // 编码码率,192kbps是目前的标准
format: 'mp3' // 录音文件格式,这里设置为MP3
};
recorderManager.start(options);
recorderManager.onStart(() => {
console.log('录音开始');
this.setData({
isRecording: true
});
});
recorderManager.onError((errMsg) => {
console.error('录音失败:', errMsg);
});
},
stopRecording: function() {
recorderManager.stop();
recorderManager.onStop((res) => {
console.log('录音结束', res.tempFilePath);
this.setData({
isRecording: false,
tempFilePath: res.tempFilePath
});
});
recorderManager.onError((errMsg) => {
console.error('停止录音失败:', errMsg);
});
}
});
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/adfd0e9929684d48a11415f216715d3b.png)