|
|
|
@ -1,30 +1,43 @@ |
|
|
|
import Recorder from 'js-audio-recorder'; |
|
|
|
import UNI_APP from '@/.env.js'; |
|
|
|
|
|
|
|
let rc = null; |
|
|
|
let duration = 0; |
|
|
|
let chunks = []; |
|
|
|
let stream = null; |
|
|
|
let start = () => { |
|
|
|
if (rc != null) { |
|
|
|
close(); |
|
|
|
return navigator.mediaDevices.getUserMedia({ audio: true }).then(audioStream => { |
|
|
|
const startTime = new Date().getTime(); |
|
|
|
chunks = []; |
|
|
|
stream = audioStream; |
|
|
|
rc = new MediaRecorder(stream) |
|
|
|
rc.ondataavailable = (e) => { |
|
|
|
console.log("ondataavailable") |
|
|
|
chunks.push(e.data) |
|
|
|
} |
|
|
|
rc = new Recorder(); |
|
|
|
return rc.start(); |
|
|
|
} |
|
|
|
rc.onstop = () => { |
|
|
|
duration = (new Date().getTime() - startTime) / 1000; |
|
|
|
console.log("时长:", duration) |
|
|
|
} |
|
|
|
rc.start() |
|
|
|
}) |
|
|
|
|
|
|
|
let pause = () => { |
|
|
|
rc.pause(); |
|
|
|
} |
|
|
|
|
|
|
|
let close = () => { |
|
|
|
rc.destroy(); |
|
|
|
rc = null; |
|
|
|
stream.getTracks().forEach((track) => { |
|
|
|
track.stop() |
|
|
|
}) |
|
|
|
rc.stop() |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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'; |
|
|
|
setTimeout(() => { |
|
|
|
const newbolb = new Blob(chunks, { 'type': 'audio/mpeg' }); |
|
|
|
const name = new Date().getDate() + '.mp3'; |
|
|
|
const file = new File([newbolb], name) |
|
|
|
console.log("upload") |
|
|
|
uni.uploadFile({ |
|
|
|
url: UNI_APP.BASE_URL + '/file/upload', |
|
|
|
header: { |
|
|
|
@ -39,7 +52,7 @@ let upload = () => { |
|
|
|
reject(r.message); |
|
|
|
} else { |
|
|
|
const data = { |
|
|
|
duration: parseInt(rc.duration), |
|
|
|
duration: parseInt(duration), |
|
|
|
url: r.data |
|
|
|
} |
|
|
|
resolve(data); |
|
|
|
@ -49,12 +62,12 @@ let upload = () => { |
|
|
|
reject(e); |
|
|
|
} |
|
|
|
}) |
|
|
|
}, 100) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
export { |
|
|
|
start, |
|
|
|
pause, |
|
|
|
close, |
|
|
|
upload |
|
|
|
} |