This commit is contained in:
liuye
2024-07-18 11:37:56 +08:00
parent 0aec948fe9
commit 2b3ae98025

View File

@@ -84,7 +84,9 @@
<script> <script>
import {mapActions, mapState} from "vuex"; import {mapActions, mapState} from "vuex";
const recorderManager = uni.getRecorderManager(); import Recorder from 'recorder-core'
import 'recorder-core/src/engine/mp3'
import 'recorder-core/src/engine/mp3-engine'
const innerAudioContext = uni.createInnerAudioContext(); const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true; innerAudioContext.autoplay = true;
export default { export default {
@@ -98,6 +100,7 @@ export default {
backgroundSize: 'cover', backgroundSize: 'cover',
}, },
recorderManager: null, recorderManager: null,
recorder: null,
type: 'text', type: 'text',
isStart: false, isStart: false,
content: '', content: '',
@@ -126,9 +129,6 @@ export default {
...mapState(['user', 'token']), ...mapState(['user', 'token']),
}, },
onLoad() { onLoad() {
recorderManager.onStop((res)=> {
this.upLoad(res.tempFilePath)
});
}, },
mounted() { mounted() {
this.getAiTypeList() this.getAiTypeList()
@@ -144,42 +144,72 @@ export default {
methods: { methods: {
...mapActions(['autoLogin', 'getUserInfo']), ...mapActions(['autoLogin', 'getUserInfo']),
startRecord() { startRecord() {
if(this.isFlag) return // if(this.isFlag) return
this.isStart = true // this.isStart = true
uni.authorize({ // uni.authorize({
scope: 'scope.record', // scope: 'scope.record',
success() { // success() {
// // const options = {
// // duration: 10000,
// // sampleRate: 44100,
// // numberOfChannels: 1,
// // encodeBitRate: 192000,
// // format: 'aac',
// // frameSize: 50
// // }
// const options = { // const options = {
// duration: 10000, // duration: 60000,
// sampleRate: 44100, // sampleRate: 16000,
// numberOfChannels: 1, // format: 'mp3'
// encodeBitRate: 192000,
// format: 'aac',
// frameSize: 50
// } // }
const options = { // recorderManager.start(options);
duration: 60000,
sampleRate: 16000,
format: 'mp3'
}
recorderManager.start(options);
}, // },
fail(err) { // fail(err) {
this.isStart = false // this.isStart = false
uni.showModal({ // uni.showModal({
title: "提示", // title: "提示",
content: "您的录音权限未开启", // content: "您的录音权限未开启",
// })
// }
// })
this.isStart = true
this.recorder = Recorder({
type: 'mp3',
sampleRate: 16000,
bitRate: 16
}) })
}
this.recorder.open(() => {
this.recorder.start()
}) })
}, },
endRecord() { endRecord() {
this.recorder.stop((blob) => {
var formData = new FormData();
formData.append('file', blob, 'filename.mp3');
this.$http.post('/admin/file/add', formData, {
withoutToken: true,
}).then((res) => {
uni.hideLoading()
if (res?.data) {
this.voiceUrl = res.data[0].split(';')[0]
this.voiceId = res.data[0].split(';')[1]
this.isStart = false this.isStart = false
setTimeout(() => { this.sendVoice()
this.isFlag = false }
}, 6000) }).catch(res => {
recorderManager.stop(); this.$u.toast(res)
uni.hideLoading()
})
this.recorder.close()
this.recorder = null
}, msg => {
console.log('录音失败:' + msg)
this.recorder.close()
this.recorder = null
})
}, },
microPhone() { microPhone() {
this.type = 'voice' this.type = 'voice'