Last touches for new audiowave integration

This commit is contained in:
Koper
2023-07-21 18:08:42 +07:00
parent 863a04094b
commit f4235147aa
5 changed files with 108 additions and 92 deletions

View File

@@ -1,44 +1,51 @@
// Override the startRecording method so we can pass the desired stream
// Checkout: https://github.com/katspaugh/wavesurfer.js/blob/fa2bcfe/src/plugins/record.ts
import RecordPlugin from "wavesurfer.js/dist/plugins/record"
import RecordPlugin from "wavesurfer.js/dist/plugins/record";
const MIME_TYPES = ['audio/webm', 'audio/wav', 'audio/mpeg', 'audio/mp4', 'audio/mp3']
const findSupportedMimeType = () => MIME_TYPES.find((mimeType) => MediaRecorder.isTypeSupported(mimeType))
const MIME_TYPES = [
"audio/webm",
"audio/wav",
"audio/mpeg",
"audio/mp4",
"audio/mp3",
];
const findSupportedMimeType = () =>
MIME_TYPES.find((mimeType) => MediaRecorder.isTypeSupported(mimeType));
class CustomRecordPlugin extends RecordPlugin {
static create(options) {
return new CustomRecordPlugin(options || {})
return new CustomRecordPlugin(options || {});
}
startRecording(stream) {
this.preventInteraction()
this.cleanUp()
this.preventInteraction();
this.cleanUp();
const onStop = this.render(stream)
const onStop = this.render(stream);
const mediaRecorder = new MediaRecorder(stream, {
mimeType: this.options.mimeType || findSupportedMimeType(),
audioBitsPerSecond: this.options.audioBitsPerSecond,
})
const recordedChunks = []
});
const recordedChunks = [];
mediaRecorder.addEventListener('dataavailable', (event) => {
mediaRecorder.addEventListener("dataavailable", (event) => {
if (event.data.size > 0) {
recordedChunks.push(event.data)
recordedChunks.push(event.data);
}
})
});
mediaRecorder.addEventListener('stop', () => {
onStop()
this.loadBlob(recordedChunks, mediaRecorder.mimeType)
this.emit('stopRecording')
})
mediaRecorder.addEventListener("stop", () => {
onStop();
this.loadBlob(recordedChunks, mediaRecorder.mimeType);
this.emit("stopRecording");
});
mediaRecorder.start()
mediaRecorder.start();
this.emit('startRecording')
this.emit("startRecording");
this.mediaRecorder = mediaRecorder
this.mediaRecorder = mediaRecorder;
}
}
export default CustomRecordPlugin;
export default CustomRecordPlugin;