From 8fc60ef327305144eabcd0b0ac69252dda4173b1 Mon Sep 17 00:00:00 2001 From: Rizwan Dudekula Date: Thu, 23 May 2019 23:11:15 +0530 Subject: [PATCH 1/2] to support audio autoplay in google chrome --- text-to-speech/synthesize.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/text-to-speech/synthesize.js b/text-to-speech/synthesize.js index 73d12c98..9ce1936a 100644 --- a/text-to-speech/synthesize.js +++ b/text-to-speech/synthesize.js @@ -55,7 +55,14 @@ module.exports = function synthesize(options) { audio.crossOrigin = 'anonymous'; audio.src = url + '/v1/synthesize?' + qs.stringify(pick(options, QUERY_PARAMS_ALLOWED)); if (options.autoPlay !== false) { - audio.play(); + var playPromise = audio.play(); + if (playPromise !== undefined) { + playPromise.then(_ => { + // console.log("autoPlay promise resolved") + }).catch(error => { + throw new Error("Watson TextToSpeech: autoplay error:"+ error) + }); + } } return audio; }; From 83031fbc227f0bcd4959a3be62dd4ecc2bfa6073 Mon Sep 17 00:00:00 2001 From: Rizwan Dudekula Date: Thu, 23 May 2019 23:38:14 +0530 Subject: [PATCH 2/2] npm run autofix- changes --- text-to-speech/synthesize.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/text-to-speech/synthesize.js b/text-to-speech/synthesize.js index 9ce1936a..4ac56d85 100644 --- a/text-to-speech/synthesize.js +++ b/text-to-speech/synthesize.js @@ -57,11 +57,13 @@ module.exports = function synthesize(options) { if (options.autoPlay !== false) { var playPromise = audio.play(); if (playPromise !== undefined) { - playPromise.then(_ => { - // console.log("autoPlay promise resolved") - }).catch(error => { - throw new Error("Watson TextToSpeech: autoplay error:"+ error) - }); + playPromise + .then(() => { + // console.log("autoPlay promise resolved") + }) + .catch(error => { + throw new Error('Watson TextToSpeech: autoplay error:' + error); + }); } } return audio;