| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- <template>
- <view class="container">
- <view class="record-btn-layer">
- <button
- :class="[longPress === '1' ? 'record-btn-1' : 'record-btn-2', VoiceTitle === '松开手指,取消录音' ? 'record-btn-3' : '']"
- @longpress="longpressBtn"
- @touchend="touchendBtn"
- @touchmove="handleTouchMove"
- >
- <text>{{ VoiceText }}</text>
- </button>
- </view>
- <!-- 语音音阶动画 -->
- <view :class="VoiceTitle != '松开手指,取消录音' ? 'prompt-layer prompt-layer-1' : 'prompt-layer1 prompt-layer-1'" v-if="longPress == '2'">
- <view class="prompt-loader">
- <view class="em" v-for="(item, index) in 15" :key="index"></view>
- </view>
- <text class="span">{{ VoiceTitle }}</text>
- </view>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- // 1显示 按住说话 2显示 说话中
- longPress: '1',
- //录音时长
- recordingTime: 0,
- // 定时器标识
- recordingTimer: null,
- //录音最大值ms 60000/1分钟
- duration: 60000,
- //音频路径
- tempFilePath: '',
- //记录长按录音开始点信息,用于后面计算滑动距离。
- startPoint: {},
- //发送锁,当为true时上锁,false时解锁发送
- sendLock: true,
- VoiceTitle: '松手结束录音',
- VoiceText: '按住说话',
- recorderManager: uni.getRecorderManager()
- }
- },
- methods: {
- // 长按录音事件
- longpressBtn(e) {
- //记录长按时开始点信息,后面用于计算上划取消时手指滑动的距离
- this.startPoint = e.touches[0]
- this.longPress = '2'
- this.VoiceText = '说话中...'
- this.recordingTime = 0
- this.recordingTimer = setInterval(() => {
- this.recordingTime++
- }, 1000)
- this.recorderManager.onStop((res) => {
- clearInterval(this.recordingTimer)
- uni.showModal({
- title: '提示',
- content: `已达最大录音时间${this.duration / 1000}秒`,
- showCancel: false,
- success: (result) => {
- if (result.confirm) {
- this.handleUpLoading(res)
- }
- }
- })
- // this.tempFilePath = res.tempFilePath
- })
- const options = {
- duration: this.duration, // 指定录音的时长,单位 ms
- sampleRate: 16000, // 采样率
- numberOfChannels: 1, // 录音通道数
- encodeBitRate: 96000, // 编码码率
- format: 'mp3', // 音频格式,有效值 aac/mp3
- frameSize: 10 // 指定帧大小,单位 KB
- }
- this.recorderManager.start(options)
- // 监听音频开始事件
- this.sendLock = false //长按时是不上锁的。
- this.recorderManager.onStart((res) => {})
- },
- // 长按松开录音事件
- touchendBtn() {
- this.longPress = '1'
- this.VoiceText = '按住说话'
- this.VoiceTitle = '松手结束录音'
- this.recorderManager.onStop((res) => {
- clearInterval(this.recordingTimer)
- this.handleUpLoading(res)
- })
- this.recorderManager.stop() //结束录音
- },
- handleUpLoading(res) {
- if (this.sendLock) {
- //上锁不发送
- } else {
- //解锁发送,发送网络请求
- if (res.duration < 1500)
- uni.showToast({
- title: '录音时间太短',
- icon: 'none',
- duration: 1000
- })
- else {
- uni.showLoading({
- title: '上传中'
- })
- uni.uploadFile({
- url: `https://chtech.ncjti.edu.cn/campusMaintenance/repair-api/repair/api/repairRecord/uploadFile`,
- filePath: res.tempFilePath,
- name: 'file',
- header: {
- token: uni.getStorageSync('repairsUserInfo').token,
- user_head: uni.getStorageSync('repairsUserInfo').userhead
- },
- success: (uploadFileRes) => {
- // console.log(JSON.parse(uploadFileRes.data))
- this.tempFilePath = JSON.parse(uploadFileRes.data).data.resultUrl
- this.$emit('getTempFilePath', this.tempFilePath, this.recordingTime)
- uni.hideLoading()
- },
- fail: () => {
- uni.showToast({
- title: '上传失败',
- icon: 'error'
- })
- }
- })
- }
- }
- },
- // 取消录音
- handleTouchMove(e) {
- //touchmove时触发
- var moveLenght = e.touches[e.touches.length - 1].clientY - this.startPoint.clientY //移动距离
- if (Math.abs(moveLenght) > 70) {
- this.VoiceTitle = '松开手指,取消录音'
- this.VoiceText = '松开手指,取消录音'
- //触发了上滑取消录音,上锁
- this.sendLock = true
- } else {
- this.VoiceTitle = '松手结束录音'
- this.VoiceText = '松手结束录音'
- //上划距离不足,依然可以发送,不上锁
- this.sendLock = false
- }
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .container {
- width: 100%;
- height: 100%;
- .record-btn-layer {
- display: flex;
- justify-content: center;
- align-items: center;
- width: 100%;
- height: 100%;
- button {
- width: 50%;
- height: 50px;
- line-height: 50px;
- text-align: center;
- font-size: 14px;
- border-radius: 8px;
- transition: all 0.1s;
- }
- button::after {
- border: none;
- transition: all 0.1s;
- }
- .record-btn-1 {
- background-color: #6fb6b8;
- color: #fff;
- }
- .record-btn-2 {
- color: #0061ff;
- transition: all 0.3s;
- }
- .record-btn-3 {
- background-color: #fb5353;
- color: white;
- }
- }
- }
- /* 提示小弹窗 */
- .prompt-layer {
- border-radius: 15px;
- background: #95eb6c;
- padding: 8px 16px;
- box-sizing: border-box;
- position: absolute;
- left: 50%;
- height: 11vh;
- transform: translateX(-50%);
- transition: all 0.3s;
- }
- .prompt-layer::after {
- content: '';
- display: block;
- border: 12px solid rgba(0, 0, 0, 0);
- border-radius: 10rpx;
- border-top-color: #95eb6c;
- position: absolute;
- bottom: -46rpx;
- left: 50%;
- transform: translateX(-50%);
- transition: all 0.3s;
- }
- //取消动画
- .prompt-layer1 {
- border-radius: 15px;
- background: #fb5353;
- padding: 8px 16px;
- box-sizing: border-box;
- position: absolute;
- left: 50%;
- height: 11vh;
- transform: translateX(-50%);
- transition: all 0.3s;
- }
- .prompt-layer1::after {
- content: '';
- display: block;
- border: 12px solid rgba(0, 0, 0, 0);
- border-radius: 10rpx;
- border-top-color: #fb5353;
- position: absolute;
- bottom: -46rpx;
- left: 50%;
- transform: translateX(-50%);
- transition: all 0.3s;
- }
- .prompt-layer-1 {
- font-size: 12px;
- width: 150px;
- text-align: center;
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- top: -400rpx;
- .p {
- color: #000000;
- }
- .span {
- color: rgba(0, 0, 0, 0.6);
- }
- }
- /* 语音音阶------------- */
- .prompt-loader {
- width: 96px;
- height: 20px;
- display: flex;
- align-items: center;
- justify-content: space-between;
- margin-bottom: 6px;
- }
- .prompt-loader .em {
- display: block;
- background: #333333;
- width: 1px;
- height: 10%;
- margin-right: 2.5px;
- float: left;
- }
- .prompt-loader .em:last-child {
- margin-right: 0px;
- }
- .prompt-loader .em:nth-child(1) {
- animation: load 2.5s 1.4s infinite linear;
- }
- .prompt-loader .em:nth-child(2) {
- animation: load 2.5s 1.2s infinite linear;
- }
- .prompt-loader .em:nth-child(3) {
- animation: load 2.5s 1s infinite linear;
- }
- .prompt-loader .em:nth-child(4) {
- animation: load 2.5s 0.8s infinite linear;
- }
- .prompt-loader .em:nth-child(5) {
- animation: load 2.5s 0.6s infinite linear;
- }
- .prompt-loader .em:nth-child(6) {
- animation: load 2.5s 0.4s infinite linear;
- }
- .prompt-loader .em:nth-child(7) {
- animation: load 2.5s 0.2s infinite linear;
- }
- .prompt-loader .em:nth-child(8) {
- animation: load 2.5s 0s infinite linear;
- }
- .prompt-loader .em:nth-child(9) {
- animation: load 2.5s 0.2s infinite linear;
- }
- .prompt-loader .em:nth-child(10) {
- animation: load 2.5s 0.4s infinite linear;
- }
- .prompt-loader .em:nth-child(11) {
- animation: load 2.5s 0.6s infinite linear;
- }
- .prompt-loader .em:nth-child(12) {
- animation: load 2.5s 0.8s infinite linear;
- }
- .prompt-loader .em:nth-child(13) {
- animation: load 2.5s 1s infinite linear;
- }
- .prompt-loader .em:nth-child(14) {
- animation: load 2.5s 1.2s infinite linear;
- }
- .prompt-loader .em:nth-child(15) {
- animation: load 2.5s 1.4s infinite linear;
- }
- @keyframes load {
- 0% {
- height: 10%;
- }
- 50% {
- height: 100%;
- }
- 100% {
- height: 10%;
- }
- }
- /* 语音音阶-------------------- */
- .prompt-layer-2 {
- top: -40px;
- .text {
- color: rgba(0, 0, 0, 1);
- font-size: 12px;
- }
- }
- </style>
|