Skip to content

Instantly share code, notes, and snippets.

@triloknagvenkar
Created December 11, 2018 16:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save triloknagvenkar/3911ab174c41cc98fab444ade233dac8 to your computer and use it in GitHub Desktop.
Save triloknagvenkar/3911ab174c41cc98fab444ade233dac8 to your computer and use it in GitHub Desktop.
This function is used to request the microphone permission and on receiving the data for the time it will create a multipart upload
audioStreamInitialize() {
/*
Feature detecting is a simple check for the existence of "navigator.mediaDevices.getUserMedia"
To use the microphone. we need to request permission.
The parameter to getUserMedia() is an object specifying the details and requirements for each type of media you want to access.
To use microphone it shud be {audio: true}
*/
navigator.mediaDevices.getUserMedia(self.audioConstraints)
.then(function(stream) {
/*
Creates a new MediaRecorder object, given a MediaStream to record.
*/
self.recorder = new MediaRecorder(stream);
/*
Called to handle the dataavailable event, which is periodically triggered each time timeslice milliseconds of media have been recorded
(or when the entire media has been recorded, if timeslice wasn't specified).
The event, of type BlobEvent, contains the recorded media in its data property.
You can then collect and act upon that recorded media data using this event handler.
*/
self.recorder.addEventListener('dataavailable', function(e) {
var normalArr = [];
/*
Here we push the stream data to an array for future use.
*/
self.recordedChunks.push(e.data);
normalArr.push(e.data);
/*
here we create a blob from the stream data that we have received.
*/
var blob = new Blob(normalArr, {
type: 'audio/webm'
});
/*
if the length of recordedChunks is 1 then it means its the 1st part of our data.
So we createMultipartUpload which will return an upload id.
Upload id is used to upload the other parts of the stream
else.
It Uploads a part in a multipart upload.
*/
if (self.recordedChunks.length == 1) {
self.startMultiUpload(blob, self.filename)
} else {
/*
self.incr is basically a part number.
Part number of part being uploaded. This is a positive integer between 1 and 10,000.
*/
self.incr = self.incr + 1
self.continueMultiUpload(blob, self.incr, self.uploadId, self.filename, self.bucketName);
}
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment