|
@@ -327,7 +327,51 @@ class FlutterSoundRecorder {
|
|
|
DBG,
|
|
|
"On data available : " + e.data.constructor.name
|
|
|
);
|
|
|
- chunks.push(e.data);
|
|
|
+ const fileReader = new FileReader();
|
|
|
+ fileReader.onload = function() {
|
|
|
+ // 读取完成后的回调函数
|
|
|
+ const audioContext = new AudioContext();
|
|
|
+ audioContext.decodeAudioData(fileReader.result, function(audioBuffer) {
|
|
|
+ chunks.push(audioBuffer);
|
|
|
+ var dataBuffer = [];
|
|
|
+ var audioBufferData = audioBuffer.getChannelData(0);
|
|
|
+ const data = new Float32Array(audioBufferData);
|
|
|
+ const out = new Int16Array(audioBufferData.length);
|
|
|
+ for (let i = 0; i < data.length; i++) {
|
|
|
+ const s = Math.max(-1, Math.min(1, data[i]));
|
|
|
+ out[i] = (s < 0 ? s * 0x8000 : s * 0x7FFF);
|
|
|
+ }
|
|
|
+ this.samplesMono = out;
|
|
|
+ this.maxSamples = 1152;
|
|
|
+ let remaining = this.samplesMono.length;
|
|
|
+
|
|
|
+ var mp3Encoder = new lamejs.Mp3Encoder(1,44100,128);
|
|
|
+ for (let i = 0; remaining >= 0; i += this.maxSamples) {
|
|
|
+ const left = this.samplesMono.subarray(i, i + this.maxSamples);
|
|
|
+ const mp3buffer = mp3Encoder.encodeBuffer(left);
|
|
|
+ dataBuffer.push(new Int8Array(mp3buffer));
|
|
|
+ remaining -= this.maxSamples;
|
|
|
+ }
|
|
|
+ var blob = new Blob(dataBuffer, { type: 'audio/mp3' });
|
|
|
+ var url = URL.createObjectURL(blob);
|
|
|
+ me.callbackTable[CB_recorder_log](me.callback, DBG, 'Instance Number : ' + me.instanceNo.toString())
|
|
|
+ me.setRecordURL(path, url);
|
|
|
+ var found = me.localObjects.findIndex(element => element == path);
|
|
|
+ if (found != null && found >= 0) {
|
|
|
+ me.callbackTable[CB_recorder_log](me.callback, DBG, "Found : " + found);
|
|
|
+ me.localObjects[found] = path;
|
|
|
+ } else {
|
|
|
+ me.callbackTable[CB_recorder_log](me.callback, DBG, "NOT FOUND! : " + path);
|
|
|
+ me.localObjects.push(path);
|
|
|
+ }
|
|
|
+ chunks = [];///[];
|
|
|
+ me.callbackTable[CB_recorder_log](me.callback, DBG, 'recorder stopped');
|
|
|
+ me.mediaRecorder = null;
|
|
|
+ me.callbackTable[CB_stopRecorderCompleted](me.callback, IS_RECORDER_STOPPED, true, me.getRecordURL(path));
|
|
|
+ me.callbackTable[CB_recorder_log](me.callback, DBG, '<--- mediaRecorder onstop');
|
|
|
+ });
|
|
|
+ }
|
|
|
+ fileReader.readAsArrayBuffer(e.data);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
@@ -403,49 +447,14 @@ class FlutterSoundRecorder {
|
|
|
DBG,
|
|
|
"---> mediaRecorder onstop"
|
|
|
);
|
|
|
- var blob;
|
|
|
- if (path.indexOf("@") > 0) {
|
|
|
- blob = new Blob(chunks, { type: mime_types[codec] });
|
|
|
- } else {
|
|
|
- var encoder = new WavAudioEncoder(16000, 2);
|
|
|
- encoder.encode(chunks);
|
|
|
- blob = encoder.finish();
|
|
|
- }
|
|
|
+ var blob = new Blob(chunks, { type: mime_types[codec] });
|
|
|
var url = URL.createObjectURL(blob);
|
|
|
me.callbackTable[CB_recorder_log](
|
|
|
me.callback,
|
|
|
DBG,
|
|
|
"Instance Number : " + me.instanceNo.toString()
|
|
|
);
|
|
|
-
|
|
|
me.setRecordURL(path, url);
|
|
|
-
|
|
|
- var found = me.localObjects.findIndex((element) => element == path);
|
|
|
- if (found != null && found >= 0) {
|
|
|
- me.callbackTable[CB_recorder_log](me.callback, DBG, "Found : " + found);
|
|
|
- me.localObjects[found] = path;
|
|
|
- } else {
|
|
|
- me.callbackTable[CB_recorder_log](
|
|
|
- me.callback,
|
|
|
- DBG,
|
|
|
- "NOT FOUND! : " + path
|
|
|
- );
|
|
|
- me.localObjects.push(path);
|
|
|
- }
|
|
|
- chunks = null; ///[];
|
|
|
- me.callbackTable[CB_recorder_log](me.callback, DBG, "recorder stopped");
|
|
|
- me.mediaRecorder = null;
|
|
|
- me.callbackTable[CB_stopRecorderCompleted](
|
|
|
- me.callback,
|
|
|
- IS_RECORDER_STOPPED,
|
|
|
- true,
|
|
|
- me.getRecordURL(path)
|
|
|
- );
|
|
|
- me.callbackTable[CB_recorder_log](
|
|
|
- me.callback,
|
|
|
- DBG,
|
|
|
- "<--- mediaRecorder onstop"
|
|
|
- );
|
|
|
};
|
|
|
//});
|
|
|
}
|