Close recorder on switch away, only send after finish clicked

pull/27/head^2
Scott Nonnenberg 7 years ago
parent 3bd95a2ff6
commit 584e932891

@ -1,4 +1,4 @@
/* global Whisper, moment, WebAudioRecorder */ /* global $, Whisper, moment, WebAudioRecorder */
/* eslint-disable more/no-then */ /* eslint-disable more/no-then */
@ -14,6 +14,10 @@
initialize() { initialize() {
this.startTime = Date.now(); this.startTime = Date.now();
this.interval = setInterval(this.updateTime.bind(this), 1000); this.interval = setInterval(this.updateTime.bind(this), 1000);
this.onSwitchAwayBound = this.onSwitchAway.bind(this);
$(window).on('blur', this.onSwitchAwayBound);
this.start(); this.start();
}, },
events: { events: {
@ -21,6 +25,9 @@
'click .finish': 'finish', 'click .finish': 'finish',
close: 'close', close: 'close',
}, },
onSwitchAway() {
this.close();
},
updateTime() { updateTime() {
const duration = moment.duration(Date.now() - this.startTime, 'ms'); const duration = moment.duration(Date.now() - this.startTime, 'ms');
const minutes = `${Math.trunc(duration.asMinutes())}`; const minutes = `${Math.trunc(duration.asMinutes())}`;
@ -58,17 +65,23 @@
this.remove(); this.remove();
this.trigger('closed'); this.trigger('closed');
$(window).off('blur', this.onSwitchAwayBound);
}, },
finish() { finish() {
this.clickedFinish = true;
this.recorder.finishRecording(); this.recorder.finishRecording();
this.close(); this.close();
}, },
handleBlob(recorder, blob) { handleBlob(recorder, blob) {
if (blob) { if (blob && this.clickedFinish) {
this.trigger('send', blob); this.trigger('send', blob);
} else {
this.close();
} }
}, },
start() { start() {
this.clickedFinish = false;
this.context = new AudioContext(); this.context = new AudioContext();
this.input = this.context.createGain(); this.input = this.context.createGain();
this.recorder = new WebAudioRecorder(this.input, { this.recorder = new WebAudioRecorder(this.input, {
@ -76,7 +89,7 @@
workerDir: 'js/', // must end with slash workerDir: 'js/', // must end with slash
}); });
this.recorder.onComplete = this.handleBlob.bind(this); this.recorder.onComplete = this.handleBlob.bind(this);
this.recorder.onError = this.onError; this.recorder.onError = this.onError.bind(this);
navigator.webkitGetUserMedia( navigator.webkitGetUserMedia(
{ audio: true }, { audio: true },
stream => { stream => {

Loading…
Cancel
Save