> For a complete documentation index, fetch https://docs.voximplant.ai/llms.txt

# RealtimeTTSPlayer

XAI provides a real-time Grok TTS player that streams generated audio to calls, WebSockets, and other VoxEngine media units.

Use `XAI.createRealtimeTTSPlayer(...)` to create a `RealtimeTTSPlayer`. Send `text.delta`, `text.done`, and `text.clear` messages to stream utterances and handle barge-in without reconnecting.

VoxEngine uses PCM audio at 16000 Hz. Keep the adapter audio format when configuring the xAI connection.

## Related guides

#### [Grok TTS](/feature-guides/speech/grok-tts)

Configure Grok voices, speech tags, streaming text, and barge-in.

#### [Grok full cascade](/voice-ai-orchestration/bring-your-own-llm/grok-full-cascade)

Use Grok TTS as the speech output layer in a complete Voice AI pipeline.

#### [Realtime TTS pattern](/getting-started/enhance-and-deploy/telephony-and-voice-ai#run-tts-in-voxengine)

See where realtime speech synthesis fits into VoxEngine call flows.

## Contents

* [RealtimeTTSPlayerParameters](/api-reference/voxengine/xai#createrealtimettsplayer): API key, xAI connection options, and WebSocket controls.
* [RealtimeTTSPlayer](#realtimettsplayer): runtime player object returned by the factory.
* [Methods](#methods): send streaming text and clear the provider buffer.
* [Inherited Methods](#inherited-methods): playback and media methods inherited from the shared player base.
* [PlayerEvents](#playerevents): player event names, payloads, and playback timing.

## RealtimeTTSPlayer

## Methods

### clearBuffer

Clears a [XAI.RealtimeTTSPlayer](#realtimettsplayer) buffer.

```ts
clearBuffer(): void
```

**Parameters**

This method does not accept parameters.

**Returns**

| Type   | Description              |
| ------ | ------------------------ |
| `void` | Does not return a value. |

### send

Send message object to the xAI provider context.

```ts
send(parameters: Object): void
```

**Parameters**

| Parameter    | Type     | Req. | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| ------------ | -------- | ---- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `parameters` | `Object` | ✓    | Send `text.delta` to append text, `text.done` to finish the current utterance, or `text.clear` to cancel it for barge-in. The WebSocket remains open for subsequent utterances. Preserve spaces and punctuation across text.delta chunks. For VoxEngine barge-in, clearBuffer() cancels the provider utterance and clears queued playback audio.  See the [partner API reference](https://docs.x.ai/developers/model-capabilities/audio/text-to-speech#streaming-tts-websocket). |

**Returns**

| Type   | Description              |
| ------ | ------------------------ |
| `void` | Does not return a value. |

Example `parameters` messages:

**`text.delta`**

```json
{
  "type": "text.delta",
  "delta": "Hello from Grok TTS."
}
```

**`text.done`**

```json
{
  "type": "text.done"
}
```

**`text.clear`**

```json
{
  "type": "text.clear"
}
```

## Inherited Methods

`XAI.RealtimeTTSPlayer` extends the shared player base (`BasePlayer` in the typings). Those base methods use the same playback contract documented on the public [Player](/api-reference/voxengine/player) page, and they emit [PlayerEvents](#playerevents).

#### addEventListener

Inherited from the shared player base. See [Player.addEventListener](/api-reference/voxengine/player#addeventlistener).

Adds a handler for the specified [PlayerEvents](/api-reference/voxengine/player-events) event. Use only functions as handlers; anything except a function leads to the error and scenario termination when a handler is called.

```ts
addEventListener(event: PlayerEvents | string, callback: (event: object) => any): void
```

**Parameters**

| Parameter  | Type                     | Req. | Description                                   |
| ---------- | ------------------------ | ---- | --------------------------------------------- |
| `event`    | `PlayerEvents \| string` | ✓    | Event constant or event name to subscribe to. |
| `callback` | `(event: object) => any` | ✓    | Function called when the event is emitted.    |

**Returns**

| Type   | Description              |
| ------ | ------------------------ |
| `void` | Does not return a value. |

#### id

Inherited from the shared player base. See [Player.id](/api-reference/voxengine/player#id).

Returns the player's id.

```ts
id(): string
```

**Parameters**

This method does not accept parameters.

**Returns**

| Type     | Description                 |
| -------- | --------------------------- |
| `string` | The requested string value. |

#### pause

Inherited from the shared player base. See [Player.pause](/api-reference/voxengine/player#pause).

Pauses playback. To continue the playback use the `Player.resume` method.

```ts
pause(): void
```

**Parameters**

This method does not accept parameters.

**Returns**

| Type   | Description              |
| ------ | ------------------------ |
| `void` | Does not return a value. |

#### removeEventListener

Inherited from the shared player base. See [Player.removeEventListener](/api-reference/voxengine/player#removeeventlistener).

Removes a handler for the specified [PlayerEvents](/api-reference/voxengine/player-events) event.

```ts
removeEventListener(event: PlayerEvents | string, callback?: (event: object) => any): void
```

**Parameters**

| Parameter  | Type                     | Req. | Description                                   |
| ---------- | ------------------------ | ---- | --------------------------------------------- |
| `event`    | `PlayerEvents \| string` | ✓    | Event constant or event name to subscribe to. |
| `callback` | `(event: object) => any` | ✗    | Function called when the event is emitted.    |

**Returns**

| Type   | Description              |
| ------ | ------------------------ |
| `void` | Does not return a value. |

#### resume

Inherited from the shared player base. See [Player.resume](/api-reference/voxengine/player#resume).

Resumes playback after the `Player.pause` method is called.

```ts
resume(): void
```

**Parameters**

This method does not accept parameters.

**Returns**

| Type   | Description              |
| ------ | ------------------------ |
| `void` | Does not return a value. |

#### sendMediaTo

Inherited from the shared player base. See [Player.sendMediaTo](/api-reference/voxengine/player#sendmediato).

Starts sending media from the player to the media unit.

```ts
sendMediaTo(mediaUnit: VoxMediaUnit, parameters?: SendMediaParameters): void
```

**Parameters**

| Parameter    | Type                                    | Req. | Description |
| ------------ | --------------------------------------- | ---- | ----------- |
| `mediaUnit`  | `VoxMediaUnit`                          | ✓    |             |
| `parameters` | <code>SendMedia<wbr />Parameters</code> | ✗    |             |

**Returns**

| Type   | Description              |
| ------ | ------------------------ |
| `void` | Does not return a value. |

#### stop

Inherited from the shared player base. See [Player.stop](/api-reference/voxengine/player#stop).

Stops playback. The current player's instance is destroyed.

```ts
stop(): void
```

**Parameters**

This method does not accept parameters.

**Returns**

| Type   | Description              |
| ------ | ------------------------ |
| `void` | Does not return a value. |

#### stopMediaTo

Inherited from the shared player base. See [Player.stopMediaTo](/api-reference/voxengine/player#stopmediato).

Stops sending media from the player to the media unit.

```ts
stopMediaTo(mediaUnit: VoxMediaUnit): void
```

**Parameters**

| Parameter   | Type           | Req. | Description |
| ----------- | -------------- | ---- | ----------- |
| `mediaUnit` | `VoxMediaUnit` | ✓    |             |

**Returns**

| Type   | Description              |
| ------ | ------------------------ |
| `void` | Does not return a value. |

## PlayerEvents

These are inherited [PlayerEvents](/api-reference/voxengine/player-events). Event callbacks receive the shared player event object; `player` is the `XAI.RealtimeTTSPlayer` instance that emitted the event. Some events include additional playback fields noted below.

#### Created

Triggered when [Player](/api-reference/voxengine/player) created.

Event constant: `PlayerEvents.Created`

**Payload**

| Field    | Type                                  | Req. | Description                                                                      |
| -------- | ------------------------------------- | ---- | -------------------------------------------------------------------------------- |
| `player` | <code>RealtimeTTS<wbr />Player</code> | ✓    | The [XAI.RealtimeTTSPlayer](#realtimettsplayer) instance that emitted the event. |

#### PlaybackReady

Triggers by the `createURLPlayer` and `createTTSPlayer` methods when

1. the audio file download to the Voximplant cache is finished;
2. the audio file is found in the cache (i.e., it is in the cache before).

Event constant: `PlayerEvents.PlaybackReady`

**Payload**

| Field    | Type                                  | Req. | Description                                                                      |
| -------- | ------------------------------------- | ---- | -------------------------------------------------------------------------------- |
| `player` | <code>RealtimeTTS<wbr />Player</code> | ✓    | The [XAI.RealtimeTTSPlayer](#realtimettsplayer) instance that emitted the event. |

#### Started

Triggered when playback is started. Note that if the `createURLPlayer` method is called with the **onPause** parameter set to true, the event is not triggered; it is triggered after the `Player.resume` method call.

Event constant: `PlayerEvents.Started`

**Payload**

| Field      | Type                                  | Req. | Description                                                                      |
| ---------- | ------------------------------------- | ---- | -------------------------------------------------------------------------------- |
| `player`   | <code>RealtimeTTS<wbr />Player</code> | ✓    | The [XAI.RealtimeTTSPlayer](#realtimettsplayer) instance that emitted the event. |
| `duration` | `number`                              | ✓    | Playback duration                                                                |

#### Stopped

Triggers as a result of the `Player.stop` method call.

Event constant: `PlayerEvents.Stopped`

**Payload**

| Field    | Type                                  | Req. | Description                                                                      |
| -------- | ------------------------------------- | ---- | -------------------------------------------------------------------------------- |
| `player` | <code>RealtimeTTS<wbr />Player</code> | ✓    | The [XAI.RealtimeTTSPlayer](#realtimettsplayer) instance that emitted the event. |

#### PlaybackFinished

Triggered when playback has finished successfully or with an error.

Event constant: `PlayerEvents.PlaybackFinished`

**Payload**

| Field    | Type                                  | Req. | Description                                                                      |
| -------- | ------------------------------------- | ---- | -------------------------------------------------------------------------------- |
| `player` | <code>RealtimeTTS<wbr />Player</code> | ✓    | The [XAI.RealtimeTTSPlayer](#realtimettsplayer) instance that emitted the event. |
| `error`  | `string`                              | ✗    | Error message                                                                    |

#### Error

Triggered when playback has finished with an error.

Event constant: `PlayerEvents.Error`

**Payload**

| Field    | Type                                  | Req. | Description                                                                      |
| -------- | ------------------------------------- | ---- | -------------------------------------------------------------------------------- |
| `player` | <code>RealtimeTTS<wbr />Player</code> | ✓    | The [XAI.RealtimeTTSPlayer](#realtimettsplayer) instance that emitted the event. |
| `error`  | `string`                              | ✓    | Error message                                                                    |

#### PlaybackMarkerReached

Triggered when `Player.addMarker` is reached.

Event constant: `PlayerEvents.PlaybackMarkerReached`

**Payload**

| Field    | Type                                  | Req. | Description                                                                      |
| -------- | ------------------------------------- | ---- | -------------------------------------------------------------------------------- |
| `player` | <code>RealtimeTTS<wbr />Player</code> | ✓    | The [XAI.RealtimeTTSPlayer](#realtimettsplayer) instance that emitted the event. |
| `offset` | `number`                              | ✓    | The marker offset                                                                |

#### PlaybackBuffering

Triggered when an audio file is playing faster than it is being loaded.

Event constant: `PlayerEvents.PlaybackBuffering`

**Payload**

| Field    | Type                                  | Req. | Description                                                                      |
| -------- | ------------------------------------- | ---- | -------------------------------------------------------------------------------- |
| `player` | <code>RealtimeTTS<wbr />Player</code> | ✓    | The [XAI.RealtimeTTSPlayer](#realtimettsplayer) instance that emitted the event. |

#### AudioChunksPlaybackFinished

Triggered when an audio chunk playback is finished. Note that this event is triggered only for RealtimeTTSPlayer instances.

For [VoxTTS.RealtimeTTSPlayer](/api-reference/voxengine/vox-tts/realtime-tts-player), the optional **final** field indicates whether the completed flush ends the current phrase or is an intermediate auto-flush.

Event constant: `PlayerEvents.AudioChunksPlaybackFinished`

**Payload**

| Field                               | Type                                  | Req. | Description                                                                                                                                                                                                                                                                                                                                     |
| ----------------------------------- | ------------------------------------- | ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `player`                            | <code>RealtimeTTS<wbr />Player</code> | ✓    | The [XAI.RealtimeTTSPlayer](#realtimettsplayer) instance that emitted the event.                                                                                                                                                                                                                                                                |
| <code>timeToFirst<wbr />Byte</code> | `number`                              | ✓    | Time to first byte (TTFB) in milliseconds. Represents the delay between sending the request and receiving the first byte of audio data.                                                                                                                                                                                                         |
| `final`                             | `boolean`                             | ✗    | Whether the completed flush ends the current phrase. Present only for [VoxTTS.RealtimeTTSPlayer](/api-reference/voxengine/vox-tts/realtime-tts-player) (`true` or `false`); omitted for other RealtimeTTS providers. - `true` — final (normal) flush; phrase playback is complete. - `false` — intermediate auto-flush; more audio is expected. |