only records audio now

This commit is contained in:
nikvaessen 2016-07-11 18:52:02 +02:00
parent 256cd88a48
commit e6df3e3ecf
2 changed files with 67 additions and 11 deletions

View File

@ -0,0 +1,3 @@
/**
* Created by workingnik on 10/07/16.
*/

View File

@ -3,7 +3,10 @@
/**
* options passed along when the MediaRecorder gets created
*/
const MEDIA_RECORDING_OPTIONS = {mimeType: "audio/webm"};
const AUDIO_WEBM = "audio/webm";
const AUDIO_OGG = "audio/ogg";
const MEDIA_RECORDING_OPTIONS_CHROME = {mimeType: AUDIO_WEBM}; //for chrome
const MEDIA_RECORDING_OPTIONS_FIREFOX = {mimeType: AUDIO_OGG}; //for ff
/**
* File type to convert data into when it's downloaded
@ -22,6 +25,24 @@ var TrackRecorder = function(track){
this.data = null;
};
/**
* Determines which kind of audio recording the browser supports
* chrome supports "audio/webm" and firefox supports "audio/ogg"
*/
function determineCorrectFileType()
{
if(MediaRecorder.isTypeSupported(AUDIO_WEBM)) {
return AUDIO_WEBM;
}
else if(MediaRecorder.isTypeSupported(AUDIO_OGG)) {
return AUDIO_OGG;
}
else {
throw new Error("unable to create a MediaRecorder with the" +
"right mimetype!")
}
}
/**
* main exported object of the file, holding all
* relevant functions and variables for the outside world
@ -29,7 +50,10 @@ var TrackRecorder = function(track){
var audioRecorder= {
// array of TrackRecorders, where each trackRecorder
// holds the JitsiTrack, MediaRecorder and recorder data
recorders: []
recorders: [],
//get which file type is supported by the current browser
fileType: determineCorrectFileType()
};
/**
@ -51,17 +75,22 @@ audioRecorder.startAudioRecording = function () {
//loop through all stored tracks and create a mediaRecorder for each stream
audioRecorder.recorders.forEach(function(trackRecorder){
//creates the recorder if not already created
if(!trackRecorder.recorder) {
trackRecorder.recorder =
new MediaRecorder(trackRecorder.track.getOriginalStream(),
MEDIA_RECORDING_OPTIONS);
//function handling a dataEvent, e.g the stream gets new data
if(trackRecorder.recorder == null) {
// Create a new stream which only holds the audio track
var originalStream = trackRecorder.track.getOriginalStream();
var stream = createEmptyStream();
originalStream.getAudioTracks().forEach(function(track){
stream.addTrack(track);
});
// Create the recorder
trackRecorder.recorder = new MediaRecorder(stream,
{mimeType: audioRecorder.fileType});
// function handling a dataEvent, e.g the stream gets new data
trackRecorder.recorder.ondataavailable = function (dataEvent) {
if(dataEvent.data.size > 0) {
trackRecorder.data.push(event.data);
trackRecorder.data.push(dataEvent.data);
}
};
}
//array for holding the recorder data. Resets it when
//audio already has been recorder once
@ -92,18 +121,42 @@ audioRecorder.stopAudioRecording = function() {
*/
audioRecorder.download = function () {
audioRecorder.recorders.forEach(function (trackRecorder) {
var blob = new Blob(trackRecorder.data, FILE_TYPE);
var blob = new Blob(trackRecorder.data, {type: audioRecorder.fileType});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
document.body.appendChild(a);
a.style = "display: none";
a.href = url;
a.download = 'test.webm';
a.download = 'test.' + audioRecorder.fileType.split("/")[1];
a.click();
window.URL.revokeObjectURL(url);
});
};
/**
* Creates a empty MediaStream object which can be used
* to add MediaStreamTracks to
* @returns MediaStream
*/
function createEmptyStream() {
// Firefox supports creation of a clean empty MediaStream
// so test if it's firefox by just returning
// a new instance
try {
return new MediaStream();
}
// if it's chrome we need to make it the dirty way...
catch(e){
if(e instanceof ReferenceError)
{
return new webkitMediaStream();
}
else {
throw new Error("cannot create a clean MediaStream");
}
}
}
/**
* export the main object audioRecorder
*/