Skip to content

Commit

Permalink
Handle no speech synthesis in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk committed Nov 28, 2023
1 parent a30ab32 commit 1b3734e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/rollbar/rollbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const reportHandledError = (description: string, e: any, info?: any) => {
export const reportHandledError = (description: string, e?: any, info?: any) => {
console.error(e);
// @ts-ignore
Rollbar.error(description, e, info);
Expand Down
13 changes: 13 additions & 0 deletions src/lib/voice-playback/speak.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { reportHandledError } from "../rollbar/rollbar.tsx";

export enum SpeakLanguageEnum {
AmericanEnglish = "en-US",
Italian = "it-IT",
Expand Down Expand Up @@ -41,6 +43,17 @@ export enum SpeakLanguageEnum {
}

export const speak = (text: string, language: SpeakLanguageEnum) => {
const isSpeechSynthesisSupported =
"speechSynthesis" in window &&
typeof SpeechSynthesisUtterance !== "undefined";

if (!isSpeechSynthesisSupported) {
reportHandledError(
`Speech synthesis is not supported in this browser. Browser info: ${navigator.userAgent}`,
);
return;
}

const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = language;

Expand Down

0 comments on commit 1b3734e

Please sign in to comment.