页面布局的代码通过uni-app提供的标签,这里就不多讲,大家可以根据自己的喜好来修改页面
<template>
<view class="video">
<!-- <button @tap="startRecord">开始录音</button>
<button @tap="endRecord">停止录音</button>
<button @tap="playVoice">播放录音</button> --><view class="start flex-row">
<view class="item">
<view v-if="voicePath" class="flex-column-center" @click="reset">
<view class="voice flex-center">
<!-- <image src="../../static/sound-recording/voice.png" mode=""></image> -->
<u-icon name="mic" color="#000" size="30"></u-icon>
</view>
<text>重新录制</text>
</view>
</view>
<view class="item flex-column-center">
<view class="record flex-center">
<image v-if="isStart" src="../../static/sound-recording/play.png" mode="" @tap="startRecord">
</image>
<image v-else src="../../static/sound-recording/pause.png" mode="" @tap="endRecord"></image>
</view>
<text v-if="isTime">{{showRecordTime}}</text>
</view>
<view class="item">
<view v-if="voicePath" class="flex-column-center" @click="submit">
<view class="voice flex-center">
<u-icon name="checkbox-mark" color="#000" size="30"></u-icon>
</view>
<text>保存提交</text>
</view>
</view>
</view>
</view>
</template>
<style lang="scss">
.video {
background-color: #161618;
height: 100vh;
position: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;
}.start {
height: 500rpx;
background-color: #222224;
position: absolute;
bottom: 0;
left: 0;
right: 0;.item {
width: 33.33%;.voice {
width: 80rpx;
height: 80rpx;
background-color: aliceblue;
border-radius: 10rpx;
}.record {
width: 150rpx;
height: 150rpx;
background-color: #161618;
border: 15rpx solid #272822;
border-radius: 50%;image {
width: 80rpx;
height: 80rpx;
}
}image {
width: 40rpx;
height: 40rpx;
}text {
font-size: 28rpx;
color: #fff;
margin-top: 20rpx;
}
}
}
</style>
计算属性中方法主要是用来计算的倒计时或者说要录制的时长,这里是以秒为换算单位,大家可以自己进行换算
computed: {
showRecordTime() {
var strs = "";
var m = Math.floor(this.time / 60);
if (m < 10) strs = "0" + m;
var s = this.time % 60;
strs += (s < 10) ? ":0" + s : ":" + s;
return strs
}
},
我这里定义的是15秒录制时长,根据自己的需求来修改time的值即可
timedown: function(num) {
let that = this;
if (num == 15) {
return clearTimeout();
} else {
setTimeout(function() {
that.time = num + 1
that.timedown(num + 1);
}, 1000); //定时每秒加一
}
},
开始录制的方法(注释的地方是获取手机的录音权限,但是他仅限于微信小程序使用,app想要获取权限的方法这里就先暂时不体现了)
console.log('开始录音');
let that = this;
this.isStart = false
this.isTime = true
clearInterval(timer);
timer = setInterval(() => {
that.time += 1
if (this.time == 15) {
clearInterval(timer);
}
}, 1000); //定时每秒减+
this.recorderManager.start();// uni.authorize({
// scope: 'scope.record',
// success() {
// recorderManager.start({
// format: 'mp3'
// });
// t.getTimeInterval();
// uni.setInnerAudioOption({
// obeyMuteSwitch: false
// })
// },
// fail() {
// uni.showModal({
// content: '检测到您没打开录音功能权限,是否去设置打开?',
// confirmText: "确认",
// cancelText: '取消',
// success(res) {
// recorderManager.start();
// t.time = setInterval(this.timer, 50)
// }
// })
// }
// })
重新录制
/**
* 重新录制
*/
reset: function() {
var that = this
wx.showModal({
title: "重新录音",
content: "是否重新录制?",
success(res) {
if (res.confirm) {
that.time = 1
that.voicePath = ''
innerAudioContext.stop()
}
}
})
},
话不多说,直接上代码。复制粘贴即用
<template>
<view class="video">
<view class="start flex-row">
<view class="item">
<view v-if="voicePath" class="flex-column-center" @click="reset">
<view class="voice flex-center">
<!-- <image src="../../static/sound-recording/voice.png" mode=""></image> -->
<u-icon name="mic" color="#000" size="30"></u-icon>
</view>
<text>重新录制</text>
</view>
</view>
<view class="item flex-column-center">
<view class="record flex-center">
<image v-if="isStart" src="../../static/sound-recording/play.png" mode="" @tap="startRecord">
</image>
<image v-else src="../../static/sound-recording/pause.png" mode="" @tap="endRecord"></image>
</view>
<text v-if="isTime">{{showRecordTime}}</text>
</view>
<view class="item">
<view v-if="voicePath" class="flex-column-center" @click="submit">
<view class="voice flex-center">
<u-icon name="checkbox-mark" color="#000" size="30"></u-icon>
</view>
<text>保存提交</text>
</view>
</view>
</view>
</view>
</template>
<script>
import {
check
} from '@/api/user.js'
let timer = null
export default {
data() {
return {
id: '',
recorderManager: {},
innerAudioContext: {},
time: 1,
isStart: true,
isTime: false,
voicePath: '',
}
},
onLoad(options) {
this.id = options.id
console.log(this.id);
this.recorderManager = uni.getRecorderManager();
this.innerAudioContext = uni.createInnerAudioContext();
// 为了防止苹果手机静音无法播放
// uni.setInnerAudioOption({
// obeyMuteSwitch: false
// })
this.innerAudioContext.autoplay = true;
console.log("uni.getRecorderManager()", uni.getRecorderManager())
let self = this;
this.recorderManager.onStop(function(res) {
console.log('recorder stop' + JSON.stringify(res));
self.voicePath = res.tempFilePath;
});
},
computed: {
showRecordTime() {
var strs = "";
var m = Math.floor(this.time / 60);
if (m < 10) strs = "0" + m;
var s = this.time % 60;
strs += (s < 10) ? ":0" + s : ":" + s;
return strs
}
},
methods: {
timedown: function(num) {
let that = this;
if (num == 15) {
return clearTimeout();
} else {
setTimeout(function() {
that.time = num + 1
that.timedown(num + 1);
}, 1000); //定时每秒减一
}
},
startRecord() {
console.log('开始录音');
let that = this;
this.isStart = false
this.isTime = true
clearInterval(timer);
timer = setInterval(() => {
that.time += 1
if (this.time == 15) {
clearInterval(timer);
}
}, 1000); //定时每秒减+
this.recorderManager.start();
// uni.authorize({
// scope: 'scope.record',
// success() {
// recorderManager.start({
// format: 'mp3'
// });
// t.getTimeInterval();
// uni.setInnerAudioOption({
// obeyMuteSwitch: false
// })
// },
// fail() {
// uni.showModal({
// content: '检测到您没打开录音功能权限,是否去设置打开?',
// confirmText: "确认",
// cancelText: '取消',
// success(res) {
// recorderManager.start();
// t.time = setInterval(this.timer, 50)
// }
// })
// }
// })
},
endRecord() {
this.isStart = true
this.isTime = true
clearInterval(timer);
console.log('录音结束');
this.recorderManager.stop();
},
// 提交
submit() {
let that = this
uni.showLoading({
title: '加载中'
})
uni.uploadFile({
url: 'http://192.168.0.159:8102/api/QCloud/upload', // 仅为示例,非真实的接口地址
filePath: that.voicePath,
name: 'file',
header: {
'Authorization': uni.getStorageSync("authorization"),
'X-Token-Type': 'MT'
},
success: (res) => {
console.log(res);
that.updateUserInfo(res.data)
},
})
},
// 修改用户信息
updateUserInfo(voice) {
check({
auditType:1,
cover:voice,
}).then(res => {
console.log(res);
uni.hideLoading()
uni.showToast({
title: '提交成功',
icon: 'success'
})
uni.navigateBack({
delta: 1,
})
})
},
/**
* 重新录制
*/
reset: function() {
var that = this
wx.showModal({
title: "重新录音",
content: "是否重新录制?",
success(res) {
if (res.confirm) {
that.time = 1
that.voicePath = ''
innerAudioContext.stop()
}
}
})
},
playVoice() {
console.log('播放录音');
console.log('this.voicePath', this.voicePath);
if (this.voicePath) {
this.innerAudioContext.src = this.voicePath;
this.innerAudioContext.play();
}
},
}
}
</script>
<style lang="scss">
.video {
background-color: #161618;
height: 100vh;
position: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
.start {
height: 500rpx;
background-color: #222224;
position: absolute;
bottom: 0;
left: 0;
right: 0;
.item {
width: 33.33%;
.voice {
width: 80rpx;
height: 80rpx;
background-color: aliceblue;
border-radius: 10rpx;
}
.record {
width: 150rpx;
height: 150rpx;
background-color: #161618;
border: 15rpx solid #272822;
border-radius: 50%;
image {
width: 80rpx;
height: 80rpx;
}
}
image {
width: 40rpx;
height: 40rpx;
}
text {
font-size: 28rpx;
color: #fff;
margin-top: 20rpx;
}
}
}
</style>