|
@@ -2,7 +2,7 @@ class AudioRecord {
|
|
constructor(opt = {}) {
|
|
constructor(opt = {}) {
|
|
const defaultOptions = {
|
|
const defaultOptions = {
|
|
sampleBits: 8,
|
|
sampleBits: 8,
|
|
- sampleRate: 16000 / 6,
|
|
|
|
|
|
+ sampleRate: 16000,
|
|
onaudioprocess: function () {},
|
|
onaudioprocess: function () {},
|
|
};
|
|
};
|
|
this.options = Object.assign(defaultOptions, opt);
|
|
this.options = Object.assign(defaultOptions, opt);
|
|
@@ -17,6 +17,7 @@ class AudioRecord {
|
|
this.audioContext = null;
|
|
this.audioContext = null;
|
|
this.media = null;
|
|
this.media = null;
|
|
this.recorder = null;
|
|
this.recorder = null;
|
|
|
|
+ this.stream = null;
|
|
}
|
|
}
|
|
|
|
|
|
input(data) {
|
|
input(data) {
|
|
@@ -115,8 +116,8 @@ class AudioRecord {
|
|
}
|
|
}
|
|
return new File(
|
|
return new File(
|
|
[audioDataView],
|
|
[audioDataView],
|
|
- Math.random().toString().slice(-8) + ".mp3",
|
|
|
|
- { type: "audio/mpeg3" }
|
|
|
|
|
|
+ Math.random().toString().slice(-8) + ".wav",
|
|
|
|
+ { type: "audio/wav" }
|
|
);
|
|
);
|
|
// return new Blob([audioDataView], { type: "audio/mpeg3" });
|
|
// return new Blob([audioDataView], { type: "audio/mpeg3" });
|
|
}
|
|
}
|
|
@@ -154,9 +155,9 @@ class AudioRecord {
|
|
(stream) => {
|
|
(stream) => {
|
|
this.audioContext = new window.AudioContext();
|
|
this.audioContext = new window.AudioContext();
|
|
this.inputSampleRate = this.audioContext.sampleRate;
|
|
this.inputSampleRate = this.audioContext.sampleRate;
|
|
- console.log(this.inputSampleRate);
|
|
|
|
this.media = this.audioContext.createMediaStreamSource(stream);
|
|
this.media = this.audioContext.createMediaStreamSource(stream);
|
|
this.recorder = this.audioContext.createScriptProcessor(4096, 1, 1);
|
|
this.recorder = this.audioContext.createScriptProcessor(4096, 1, 1);
|
|
|
|
+ this.stream = stream;
|
|
//音频采集
|
|
//音频采集
|
|
this.recorder.onaudioprocess = (e) => {
|
|
this.recorder.onaudioprocess = (e) => {
|
|
this.input(e.inputBuffer.getChannelData(0));
|
|
this.input(e.inputBuffer.getChannelData(0));
|
|
@@ -186,11 +187,27 @@ class AudioRecord {
|
|
}
|
|
}
|
|
|
|
|
|
recover() {
|
|
recover() {
|
|
|
|
+ this.destroy();
|
|
this.size = 0;
|
|
this.size = 0;
|
|
this.audioBuffer = [];
|
|
this.audioBuffer = [];
|
|
this.audioContext = null;
|
|
this.audioContext = null;
|
|
this.media = null;
|
|
this.media = null;
|
|
this.recorder = null;
|
|
this.recorder = null;
|
|
|
|
+ this.stream = null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ destroy() {
|
|
|
|
+ if (this.stream && this.stream.getTracks) {
|
|
|
|
+ this.stream.getTracks().forEach((track) => track.stop());
|
|
|
|
+ this.stream = null;
|
|
|
|
+ }
|
|
|
|
+ if (
|
|
|
|
+ this.audioContext &&
|
|
|
|
+ this.audioContext.close &&
|
|
|
|
+ this.audioContext.state !== "closed"
|
|
|
|
+ ) {
|
|
|
|
+ return this.audioContext.close();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|