JavaScript API Methods

If you embed Lexora using JavaScript, you get a Lexora instance you can control programmatically: start/stop playback, toggle pause, reload audio, seek, and update UI settings at runtime.

Create a Lexora Instance

const target = document.querySelector('#lexora-target');

const lexora = new Lexora(target, {
  speech: '[YOUR_SPEECH_ID]'
});

Tip: keep a reference to the instance (lexora) so you can call methods later (for example from your own custom buttons or UI controls).

Playback Methods

Toggle play / pause / resume

This is the main “single-button” method used internally by the player UI. It behaves like a toggle:

  • If idle → starts playback
  • If playing → pauses
  • If paused → resumes
// Toggle playback
lexora.playEvent();

Start playback

Explicitly starts playback. If the audio is not loaded yet, it will be fetched and prepared before playing.

await lexora.startPlayback();

Pause playback

Pauses playback without resetting the current progress.

lexora.pause();

Resume playback

Resumes playback from the current position.

lexora.resume();

Stop playback

Stops playback and resets progress to the beginning (00:00).

lexora.stop();

Reload Methods

Reload audio

Forces the player to reload the current audio asset (same key), resetting internal state and UI, then optionally autoplays.

  • autoplay (boolean): if true, starts playback after reload
  • preload (boolean): if true, preloads audio data (no autoplay)
// Reload audio and keep it ready (no autoplay)
await lexora.reloadAudioOnly({ preload: true, autoplay: false });

// Reload audio and autoplay (note: autoplay behavior may vary by browser policies)
await lexora.reloadAudioOnly({ preload: true, autoplay: true });

Common use cases:

  • The audio was regenerated server-side.
  • You want to ensure the latest version is loaded.
  • You need a clean reset without recreating the player instance.

Seek Methods

Seek by percentage

Seeks to a percentage of the total duration (0–100).

// Jump to the middle
lexora.seekToPercent(50);

Seek by milliseconds

Seeks to an absolute position expressed in milliseconds.

// Jump to 30 seconds
lexora.seekToGlobalMs(30000);

Get current time

Returns the current playback position in milliseconds.

const ms = lexora.getGlobalCurrentTimeMs();
console.log('Current position:', ms);

Runtime Customization

Update player configuration

Updates the instance settings at runtime. This is the recommended way to change theme colors, labels, icons, and feature toggles (sticky player / playback speed) without recreating the player.

Signature

lexora.update(nextOptions, opts);

Example: update theme

lexora.update({
  style: {
    primary: '#7CD259',
    background: '#0C120D',
    foreground: '#FFFFFF',
    'widget-btn-background': '#7CD259',
    'widget-btn-foreground': '#0C120D'
  }
});

Example: update labels

lexora.update({
  labels: {
    play: 'Play',
    stop: 'Stop',
    resume: 'Resume',
    pause: 'Pause',
    speed: 'Playback speed',
    close: 'Close',
    init: 'Press play to listen',
    loading: 'Loading speech...'
  }
});

Example: toggle features

// Enable sticky mini player
lexora.update({ stickyplayer: true });

// Enable playback speed button (auto-disabled on iOS)
lexora.update({ playspeed: true });

In most integrations you won’t need the optional opts parameter.

Reading Player State

A Lexora instance exposes useful state values you can read to build custom UI logic.

  • lexora.isPlaying (boolean)
  • lexora.isPaused (boolean)
  • lexora.audioLoaded (boolean)
  • lexora.currentSpeed (number: 1, 1.5, 2)
  • lexora.totalDurationMs (number)
if (lexora.isPlaying) {
  console.log('Audio is playing');
}

if (lexora.isPaused) {
  console.log('Audio is paused');
}

onReady Callback

You can pass an onReady callback in the constructor options to run code when the player is initialized and ready.

const lexora = new Lexora(target, {
  speech: '[YOUR_SPEECH_ID]',
  onReady(instance) {
    console.log('Lexora ready:', instance);
  }
});

Best Practices

  • Use playEvent() if you want a single toggle control like the built-in UI.
  • Use startPlayback() for explicit “play now” actions.
  • Use update() for runtime theme or language changes.
  • Use seekToPercent() for simple progress controls and seekToGlobalMs() for precise seeking.