From b9a757ea08ee8b9d68e6fad40d271c6847d2a796 Mon Sep 17 00:00:00 2001 From: picklesrus Date: Mon, 27 Aug 2018 08:05:51 -0700 Subject: [PATCH] Fix speech extension in Safari (#1202) by calling getUserMedia for every listen and wait block rather than trying to reuse it. Also get rid of some code that was not actually doing anything. (#1467) --- src/extensions/scratch3_speech/index.js | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/extensions/scratch3_speech/index.js b/src/extensions/scratch3_speech/index.js index b7585de75..84e4b6e7c 100644 --- a/src/extensions/scratch3_speech/index.js +++ b/src/extensions/scratch3_speech/index.js @@ -512,12 +512,7 @@ class Scratch3SpeechBlocks { * @private */ _startListening () { - // If we've already setup the context, we can resume instead of doing all the setup again. - if (this._context) { - this._resumeListening(); - } else { - this._initListening(); - } + this._initListening(); // Force the block to timeout if we don't get any results back/the user didn't say anything. this._speechTimeoutId = setTimeout(this._stopTranscription, listenAndWaitBlockTimeoutMs); } @@ -547,18 +542,18 @@ class Scratch3SpeechBlocks { * @private */ _initializeMicrophone () { - // Safari still needs a webkit prefix for audio context - this._context = new (window.AudioContext || window.webkitAudioContext)(); + // Don't make a new context if we already made one. + if (!this._context) { + // Safari still needs a webkit prefix for audio context + this._context = new (window.AudioContext || window.webkitAudioContext)(); + } + // In safari we have to call getUserMedia every time we want to listen. Other browsers allow + // you to reuse the mediaStream. See #1202 for more context. this._audioPromise = navigator.mediaDevices.getUserMedia({ audio: true }); - const tempContext = this._context; - this._audioPromise.then(micStream => { - const microphone = tempContext.createMediaStreamSource(micStream); - const analyser = tempContext.createAnalyser(); - microphone.connect(analyser); - }).catch(e => { + this._audioPromise.then().catch(e => { log.error(`Problem connecting to microphone: ${e}`); }); }