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

View File

@@ -84,8 +84,10 @@
<script> <script>
import {mapActions, mapState} from "vuex"; import {mapActions, mapState} from "vuex";
const recorderManager = uni.getRecorderManager(); import Recorder from 'recorder-core'
const innerAudioContext = uni.createInnerAudioContext(); import 'recorder-core/src/engine/mp3'
import 'recorder-core/src/engine/mp3-engine'
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true; innerAudioContext.autoplay = true;
export default { export default {
// customNavigation: true, // customNavigation: true,
@@ -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 = { // // const options = {
// duration: 10000, // // duration: 10000,
// sampleRate: 44100, // // sampleRate: 44100,
// numberOfChannels: 1, // // numberOfChannels: 1,
// encodeBitRate: 192000, // // encodeBitRate: 192000,
// format: 'aac', // // format: 'aac',
// frameSize: 50 // // frameSize: 50
// } // // }
const options = { // const options = {
duration: 60000, // duration: 60000,
sampleRate: 16000, // sampleRate: 16000,
format: 'mp3' // format: 'mp3'
} // }
recorderManager.start(options); // 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.isStart = false this.recorder.stop((blob) => {
setTimeout(() => { var formData = new FormData();
this.isFlag = false formData.append('file', blob, 'filename.mp3');
}, 6000) this.$http.post('/admin/file/add', formData, {
recorderManager.stop(); 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.sendVoice()
}
}).catch(res => {
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'