diff --git a/im-uniapp/common/recorder-app.js b/im-uniapp/common/recorder-app.js new file mode 100644 index 0000000..dce0cbf --- /dev/null +++ b/im-uniapp/common/recorder-app.js @@ -0,0 +1,62 @@ +import UNI_APP from '@/.env.js'; + +const rc = uni.getRecorderManager(); +// 录音开始时间 +let startTime = null; + +let start = () => { + return new Promise((resolve, reject) => { + rc.onStart(() => { + startTime = new Date(); + resolve() + }); + rc.onError((e) => { + console.log(e); + reject(e) + }) + rc.start({ + format: 'mp3' // 录音格式,可选值:aac/mp3 + }); + }) +} + +let pause = () => { + rc.stop(); +} + +let close = () => { + rc.stop(); +} + +let upload = () => { + return new Promise((resolve, reject) => { + rc.onStop((wavFile, a, b) => { + uni.uploadFile({ + url: UNI_APP.BASE_URL + '/file/upload', + header: { + accessToken: uni.getStorageSync("loginInfo").accessToken + }, + filePath: wavFile.tempFilePath, + name: 'file', + success: (res) => { + const duration = (new Date().getTime() - startTime.getTime()) / 1000 + const data = { + duration: Math.round(duration), + url: JSON.parse(res.data).data + } + resolve(data); + }, + fail: (e) => { + reject(e); + } + }) + }); + }) +} + +export { + start, + pause, + close, + upload +} \ No newline at end of file diff --git a/im-uniapp/common/recorder-h5.js b/im-uniapp/common/recorder-h5.js new file mode 100644 index 0000000..1f435ca --- /dev/null +++ b/im-uniapp/common/recorder-h5.js @@ -0,0 +1,54 @@ +import Recorder from 'js-audio-recorder'; +import UNI_APP from '@/.env.js'; + +let rc = null; +let start = () => { + if(rc != null){ + close(); + } + rc = new Recorder(); + return rc.start(); +} + +let pause = () => { + rc.pause(); +} + +let close = () => { + rc.destroy(); + rc = null; +} + +let upload = () => { + return new Promise((resolve, reject) => { + const wavBlob = rc.getWAVBlob(); + const newbolb = new Blob([wavBlob], { type: 'audio/wav'}) + const name = new Date().getDate() + '.wav'; + const file = new File([newbolb], name) + uni.uploadFile({ + url: UNI_APP.BASE_URL + '/file/upload', + header: { + accessToken: uni.getStorageSync("loginInfo").accessToken + }, + file: file, + name: 'file', + success: (res) => { + const data = { + duration: parseInt(rc.duration), + url: JSON.parse(res.data).data + } + resolve(data); + }, + fail: (e) => { + reject(e); + } + }) + }) +} + +export { + start, + pause, + close, + upload +} \ No newline at end of file diff --git a/im-uniapp/components/chat-message-item/chat-message-item.vue b/im-uniapp/components/chat-message-item/chat-message-item.vue index 74be926..6f69044 100644 --- a/im-uniapp/components/chat-message-item/chat-message-item.vue +++ b/im-uniapp/components/chat-message-item/chat-message-item.vue @@ -1,13 +1,16 @@