From 7e224f3d37fd76bddc2513bde5cb6296d3b22990 Mon Sep 17 00:00:00 2001 From: jia <1451658316@qq.com> Date: Wed, 18 Oct 2023 10:59:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/index.vue | 278 +++++++++++++++++++++++++++--------------- 1 file changed, 183 insertions(+), 95 deletions(-) diff --git a/pages/index/index.vue b/pages/index/index.vue index 0ad71ce..c38a441 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -35,7 +35,7 @@ - + @@ -60,10 +60,10 @@ - + - + @@ -184,7 +184,6 @@ - @@ -275,7 +274,6 @@ touchMoveX: 0, // x轴方向移动的距离 touchMoveY: 0, // y轴方向移动的距离 risshow: false, - touchT: null, touchE: null, islong: false, @@ -283,8 +281,10 @@ isDraw: false, imageSize: { - } - + }, + audio_file: [], + currentIndex: 0, + wv: null } }, mounted() { @@ -313,6 +313,9 @@ this.avatar = e.avatar; }) + + + }, beforeDestroy() { // #ifdef APP-PLUS @@ -340,6 +343,9 @@ }) }); + + + this.list() }, watch: { @@ -630,69 +636,93 @@ } // #endif }, - // 调用讯飞语音识别 - voiceBegain() { - let _this = this; - let options = {}; - //#ifdef APP-PLUS || APP-PLUS-NVUE - options.engine = 'iFly'; - options.punctuation = false; // 是否需要标点符号 - options.timeout = 10 * 6000; //语音录入持续时长 - - plus.speech.startRecognize(options, function(s) { - _this.searchText = _this.searchText + s; - console.log(_this.searchText) //拿到语音识别的结果 - //下面是逻辑 - _this.searchValue = s; - _this.searchText = "" - - // 打印输出结果 - _this.words1 = _this.searchValue - _this.words2 = _this.searchText - - // 关闭语音 - plus.speech.stopRecognize(); - }, function(e){ - console.log('语音识别失败:'+JSON.stringify(e)); - }); - //#endif - // #ifdef H5 - alert("只有h5平台才有alert方法") - // #endif - - }, - + + //文字转语音 txtspeech(val, i) { - // xuiat({ - // text: val - // }).then(res => { - // if (res.code == 1) { - // this.talkList[i].audio_file = res.data.audio_file - // this.nums = this.talkList.length - 1 - // // this.palyaudio(res.data) - // } - // }).catch((err) => { - // console.log(err) - // }) - - ttWss({ - data: val + xuiat({ + text: val }).then(res => { - if (res.code == 0) { - this.talkList[i].audio_file = res.data.mp3 - this.nums = this.talkList.length - 1 + if (res.code == 1) { + this.talkList[i].audio_file = res.data.audio_file this.nums = this.talkList.length - 1 + // this.palyaudio(res.data) } - }).catch((err) => { console.log(err) }) + + // 暂时不用 + // let arry1; + // if (val.indexOf(',') == -1) { + // arry1 = val.split(',') + // } else if (val.indexOf(',') == -1) { + // arry1 = val.split(',') + // } else { + // arry1.push(val) + // } + + // const temp = [] + // for (let i = 0; i < arry1.length; i++) { + // temp.push( + // // 使用Promise用于异步计算 + // new Promise((resolve, reject) => { + + // ttWss({ + // data: arry1[i] + // }).then(res => { + + // return resolve(res.data.mp3) + // }).catch(err => { + // return reject(err) + // }) + // }) + // ) + // } + // Promise.all(temp).then(res => { + // // console.log(res,'2222222') + // this.audio_file = res + + // this.ScanAudio(res, this.currentIndex, 0) + // }) + }, + //语音分段 数组 条数 延时 + ScanAudio(urls, currentIndex, delayInSeconds) { + + if (currentIndex >= urls.length) { + this.palystatus = true; + return false; + } + + const music = uni.createInnerAudioContext(); + music.src = urls[currentIndex]; + console.log(urls[currentIndex]) + + console.log('播放成功', this.palystatus) + if (this.palystatus) { + this.palystatus = false + music.play(); + + } else { + + music.pause(); + + } + + music.onEnded(() => { + + music.destroy(); + setTimeout(() => { + this.ScanAudio(urls, currentIndex + 1, delayInSeconds); + }, delayInSeconds * 1000); + }); + + }, //点击事件 longclick() { if (this.touchE - this.touchT < 1000) { @@ -702,7 +732,7 @@ }, longpress(e) { // console.log(e.touches[0].pageX) - this.voiceBegain() + this.onFeedTap() this.touchT = new Date().getTime(); this.islong = true @@ -715,8 +745,22 @@ this.btnStatus = 1 recorderManager.start({ //时长5分钟,单位毫秒 - duration: 300000 + duration: 300000, + sampleRate: 16000, //采样率,有效值 8000/16000/44100 + numberOfChannels: 1, //录音通道数,有效值 1/2 + encodeBitRate: 96000, //编码码率 + format: "MP3", //音频格式,有效值 aac/mp3 + frameSize: 8, //指定帧大小 + // format: "pcm", }); + recorderManager.onFrameRecorded((res) => { + console.log(res, '111111111111') + let arr1 = res + uni.sendHostEvent('log', option, (ret) => { + //发送消息成功回调 + console.log('语音分片消息成功' + JSON.stringify(arr1)); + }); + }) this.touchStartX = e.touches[0].pageX; // 获取触摸时的原点 this.touchStartY = e.touches[0].pageY; // 获取触摸时的原点 }, @@ -729,6 +773,7 @@ end() { this.touchE = new Date().getTime(); + if (this.islong) { this.closePop() // clearInterval(this.timer1) @@ -783,7 +828,6 @@ // }, 500) - this.islong = false clearInterval(this.timer) this.btnStatus = 0 @@ -795,7 +839,7 @@ "id": '@', "content": '加载完毕', "type": 2, - "pic": this.avatarType + } this.talkList.push(data); // 异步,解决录音结束后,路径没赋值就传值,导致的路径为空 @@ -812,7 +856,10 @@ if (this.voicePath.length > 0 && this.risshow == false) { // this.huashow=true this.TabItemTap() + + throttle(this.uploadFile(this.voicePath), 1000) + } this.num = 0 @@ -835,29 +882,32 @@ }, toBase64() { + pathToBase64(this.voicePath) .then(base64 => { - const arrayBuffer = new Uint8Array(base64) - const base1 = uni.arrayBufferToBase64(arrayBuffer.split(0, 1280)) - let params = { - common: { - app_id: '2eda6c2e', - }, - business: { - language: "zh_cn", - domain: "iat", - accent: "mandarin", - vad_eos: 5000, - dwa: "wpgs", - }, - data: { - status: 2, - format: "audio/L16;rate=16000", - encoding: "raw", - audio: base1 - }, - }; - this.audioTxt = JSON.stringify(params) + + // this.$scope.$getAppWebview().children()[0].evalJS('uniEvent(base64)'); + // const arrayBuffer = new Uint8Array(base64) + // const base1 = uni.arrayBufferToBase64(arrayBuffer.split(0, 1280)) + // let params = { + // common: { + // app_id: '2eda6c2e', + // }, + // business: { + // language: "zh_cn", + // domain: "iat", + // accent: "mandarin", + // vad_eos: 5000, + // dwa: "wpgs", + // }, + // data: { + // status: 2, + // format: "audio/L16;rate=16000", + // encoding: "raw", + // audio: base1 + // }, + // }; + // this.audioTxt = JSON.stringify(params) }) .catch(error => { @@ -908,9 +958,31 @@ }, //音频播放 - palyaudio(item) { + palyaudio1(item, i) { this.palystatus = !this.palystatus + console.log(this.audio_file, '111') + if (!this.palystatus) { + this.ScanAudio(this.audio_file, this.currentIndex, 0) + } else { + this.ScanAudio(this.audio_file, this.currentIndex, 0) + // this.txtspeech(item.content, i) + } + // innerAudioContext.onError((res) => { + // console.log(res.errMsg); + // console.log(res.errCode); + // this.palystatus = true + // }); + + innerAudioContext.onEnded((res) => { + this.palystatus = true + }); + + }, + //音频播放 + + palyaudio(item, i) { + this.palystatus = !this.palystatus if (!this.palystatus) { innerAudioContext.src = item.audio_file; innerAudioContext.play() @@ -1086,7 +1158,20 @@ let that = this; let moveX = this.touchMoveX - e.touches[0].pageX; let moveY = this.touchMoveY - e.touches[0].pageY; - + // console.log(-115 > moveX && -240 < moveX,moveY) + let platform = uni.getSystemInfoSync().platform; + if(platform==='ios'){ + if (-115 > moveX && -240 < moveX && moveY < -620 && moveY > -730) { + + this.risshow = true + + this.deletespeech() + } else { + this.risshow = false + } + }else{ + + if (-115 > moveX && -240 < moveX && moveY < -359 && moveY > -480) { this.risshow = true @@ -1095,6 +1180,7 @@ } else { this.risshow = false } + } }, audiosckt() { @@ -1212,7 +1298,7 @@ "id": new Date().getTime(), "content": this.content, "type": 1, - "pic": this.avatar + "pic": this.avatarType } this.TEXT = this.content, this.talkList.push(data); @@ -1220,7 +1306,7 @@ "id": new Date().getTime(), "content": '', "type": 2, - "pic": this.avatarType + // "pic": this.avatarType }); this.n_content = ''; this.c_content = ''; @@ -1308,15 +1394,16 @@ "role": "assistant", "content": this.tempRes }) + this.nums = this.talkList.length - 1 + + this.txtspeech(this.n_content, this.talkList.length - + 1) setTimeout(() => { let that = this realThis.socketTask.close({ success(res) { console.log('关闭成功', res) - that.nums = that.talkList.length - 1 - that.txtspeech(that.n_content, that.talkList.length - - 1) realThis.wsLiveFlag = false; }, @@ -1346,7 +1433,8 @@ "id": new Date().getTime(), "content": this.content, "type": 1, - "pic": this.avatar + // "pic": this.avatar + "pic": this.avatarType } // console.log(data); this.TEXT = this.content; @@ -1462,15 +1550,15 @@ "role": "assistant", "content": this.tempRes }) + this.nums = this.talkList.length - 1 + this.txtspeech(this.n_content, this.talkList.length - + 1) setTimeout(() => { let that = this realThis.socketTask.close({ success(res) { console.log('关闭成功', res) - that.nums = that.talkList.length - 1 - that.txtspeech(that.n_content, that.talkList.length - - 1) realThis.wsLiveFlag = false; @@ -1548,7 +1636,7 @@ page { // background-color: #f5f5f5; height: 100vh; - background: url('@/static/icon/bg1.png') no-repeat; + // background: url('@/static/icon/bg1.png') no-repeat; background-size: 100% 100%; background-attachment: fixed; background-repeat: no-repeat; @@ -1665,7 +1753,7 @@ width: 100%; padding-top: var(--status-bar-height); padding-bottom: 30rpx; - background: url('@/static/icon/bg1.png'); + // background: url('@/static/icon/bg1.png'); background-size: cover; text-align: center; background-color: transparent;