Is there a way to only get the last chunk recorded when timeSlice is used?
desi-dimitrova opened this issue · comments
I have the following code which slices the recording into 1-minute chunks:
this.recorder = new RecordRTC(this.stream, {
type: 'audio',
mimeType: 'audio/wav',
recorderType: StereoAudioRecorder,
desiredSampRate: 16000,
numberOfAudioChannels: 1,
bufferSize: 16384,
audioBitsPerSecond: 128000,
disableLogs: true,
timeSlice: 60000,
ondataavailable: (blob: Blob) => {
// do something
}
});
if the recorder stops when we only have 30 seconds of audio data, is there a way to get that last 30-second recording?
Yes, you can access (individual) slices created by timeSlice using the internalRecorder
.
say you init the recorder with the above code:
this.recorder = new RecordRTC(this.stream, {
type: 'audio',
mimeType: 'audio/wav',
recorderType: StereoAudioRecorder,
desiredSampRate: 16000,
numberOfAudioChannels: 1,
bufferSize: 16384,
audioBitsPerSecond: 128000,
disableLogs: true,
timeSlice: 60000,
});
Then, you can access the internal array of sliced blobs and retrieve any (read: the last) chunk.
const internal = this.recorder.getInternalRecorder();
if (internal && internal.getArrayOfBlobs) {
const blobArray = internal.getArrayOfBlobs(); //returns array with all slices
const lastBlob = blobArray[blobArray.length - 1]; //get the last blob
const lastBlobURL = URL.createObjectURL(lastBlob)); //do something with is, such as creating an url for it
// you can also create a new blob using (for instance) all previous recorded blobs:
const bigBlob = new Blob(internal.getArrayOfBlobs(), {
type: "audio",
});
const bigBlobURL = URL.createObjectURL(bigBlob)); //do something with is, such as creating an url for it
}
Small sidenote
you write:
if the recorder stops when we only have 30 seconds of audio data, is there a way to get that last 30-second recording?
If you stop the recording before the first slice is made (so any recording less than 1 minute in your example) you can access this blob on the normal way! As can be read in the documentation:
When MediaRecorder.stop() is called, all media data which has been captured since recording began or the last time a dataavailable event occurred is delivered in a Blob; after this, capturing ends.
Thank you for the information!!