Skip to content

Commit

Permalink
Close recorder on switch away, only send after finish clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Oct 5, 2018
1 parent 3bd95a2 commit 584e932
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions js/views/recorder_view.js
@@ -1,4 +1,4 @@
/* global Whisper, moment, WebAudioRecorder */
/* global $, Whisper, moment, WebAudioRecorder */

/* eslint-disable more/no-then */

Expand All @@ -14,13 +14,20 @@
initialize() {
this.startTime = Date.now();
this.interval = setInterval(this.updateTime.bind(this), 1000);

this.onSwitchAwayBound = this.onSwitchAway.bind(this);
$(window).on('blur', this.onSwitchAwayBound);

this.start();
},
events: {
'click .close': 'close',
'click .finish': 'finish',
close: 'close',
},
onSwitchAway() {
this.close();
},
updateTime() {
const duration = moment.duration(Date.now() - this.startTime, 'ms');
const minutes = `${Math.trunc(duration.asMinutes())}`;
Expand Down Expand Up @@ -58,25 +65,31 @@

this.remove();
this.trigger('closed');

$(window).off('blur', this.onSwitchAwayBound);
},
finish() {
this.clickedFinish = true;
this.recorder.finishRecording();
this.close();
},
handleBlob(recorder, blob) {
if (blob) {
if (blob && this.clickedFinish) {
this.trigger('send', blob);
} else {
this.close();
}
},
start() {
this.clickedFinish = false;
this.context = new AudioContext();
this.input = this.context.createGain();
this.recorder = new WebAudioRecorder(this.input, {
encoding: 'mp3',
workerDir: 'js/', // must end with slash
});
this.recorder.onComplete = this.handleBlob.bind(this);
this.recorder.onError = this.onError;
this.recorder.onError = this.onError.bind(this);
navigator.webkitGetUserMedia(
{ audio: true },
stream => {
Expand Down

0 comments on commit 584e932

Please sign in to comment.