Class: TextToSpeechPlayer

TextToSpeechPlayer

TextToSpeechPlayer provides text-to-speech functionality with extensive control options. Handles voice selection, speech synthesis, text cleaning, and playback control.

Features:

  • Multiple language support
  • Automatic voice selection
  • Text cleaning and formatting
  • Playback controls (pause, resume, stop)
  • Volume control with mute option
  • Offline capability detection
  • Chrome speech bug workarounds
  • Page visibility handling

Browser Compatibility:

  • Uses Web Speech API
  • Implements Chrome-specific fixes
  • Handles browser tab switching
  • Manages page unload events

Implementation Details

Chrome Bug Workarounds:

  • Implements periodic pause/resume to prevent Chrome from stopping
  • Uses timeout to prevent indefinite speech
  • Handles voice loading race conditions

State Management:

{
    isSpeaking: boolean,    // Current speech state
    isPaused: boolean,      // Pause state
    voice: SpeechSynthesisVoice, // Selected voice
    isOfflineCapable: boolean,   // Offline support
    volume: number,         // Current volume
    previousVolume: number  // Pre-mute volume
}

Event Handling:

  • beforeunload: Stops speech on page close
  • visibilitychange: Handles tab switching
  • voiceschanged: Manages voice loading
  • utterance events: Tracks speech progress

new TextToSpeechPlayer( [options])

Creates a new TextToSpeechPlayer instance

Parameters:
Name Type Argument Description
options TextToSpeechOptions <optional>

Configuration options

Source:
Example
```javascript
const tts = new TextToSpeechPlayer({
    language: 'en-US',
    rate: 1.2,
    volume: 0.8,
    cleanText: true
});
```

Methods


activate()

Activates the TextToSpeechPlayer.

Source:

<async> initialize()

Initializes the player by loading voices and checking capabilities

Source:
Throws:

If voice loading fails or no suitable voices found

Initialization steps:

  1. Loads available voices
  2. Selects appropriate voice
  3. Checks offline capability
  4. Sets up page listeners
Type
Error
Returns:
Type
Promise.<void>

mute(enable)

Mutes or unmutes audio output

Parameters:
Name Type Description
enable boolean

True to mute, false to unmute

Source:
Example
```javascript
// Mute audio
tts.mute(true);

// Restore previous volume
tts.mute(false);
```

pauseSpeaking(enable)

Pauses or resumes speech synthesis

Parameters:
Name Type Description
enable boolean

True to pause, false to resume

Source:
Example
```javascript
// Pause speech
tts.pauseSpeaking(true);

// Resume speech
tts.pauseSpeaking(false);
```

<async> speakText(text)

Speaks the provided text

Parameters:
Name Type Description
text string

Text to be spoken

Source:
Throws:

If speech synthesis fails or times out

Processing steps:

  1. Cancels any ongoing speech
  2. Cleans input text if enabled
  3. Creates utterance with current settings
  4. Handles speech synthesis
  5. Manages timeouts and Chrome workarounds
Type
Error
Returns:
Type
Promise.<void>
Example
```javascript
await tts.speakText("Hello, world!");
```

stopSpeaking()

Stops current speech synthesis Cleans up resources and resets state

Source: