init()

The init() method initializes the session for working with the avatar and returns a promise that resolves once the avatar is ready for interaction.

Why is this important?

The init() method starts the session initialization process and connects to the necessary resources for the avatar to function. This method is essential for preparing the avatar for interaction. Once the initialization process is successfully completed, the returned promise resolves, indicating that the avatar is ready to work.

Return Value: The method returns a promise that resolves once the avatar is successfully initialized. If there is an error during initialization, the promise will be rejected.

Example usage:

try {
  await avatar.init()
  console.log('Avatar is ready to work');
} catch(e) {
  console.error('Avatar initialization failed: ', e)
}

task()

The task() method sends text for the avatar to speak and initiates its playback.

Why is this important?

The task() method is used to assign a specific task to the avatar — to speak the provided text. Once this method is called, the avatar begins to speak the text using speech synthesis. It is the primary way for the avatar to interact with the user via voice.

Arguments:

text
string
required

The text the avatar should speak.

When should you use task()?

  • When you need the avatar to say a message or give instructions.
  • In cases where you need to deliver information to the user through the avatar’s voice interface.

Example usage:

avatar.task('Hello, how can I assist you today?');

cancelAllTasks()

The cancelAllTasks() method cancels the currently active task and clears the queue of all pending tasks.

When should you use cancelAllTasks()?

  • When you need the avatar to immediately stop speaking or processing any ongoing tasks
  • In cases where you need to remove all tasks waiting in the queue.

Example usage:

avatar.cancelAllTasks();

changeVoice()

The changeVoice() method cancels all tasks and changes voice for subsequent tasks.

When should you use changeVoice()?

  • Use this method when you need the avatar to immediately stop speaking and switch to a different voice for subsequent tasks.

Example usage:

avatar.changeVoice({ engine: 'elevenlabs', voice_id: 'different_voice_id' });

attachDOMNode()

The attachDOMNode() method updates the HTML DOM element where the avatar is rendered. This allows you to dynamically change the rendering container without restarting or reinitializing the avatar.

When should you use attachDOMNode()?

  • When you need to move the avatar to a different section of the page.
  • In cases where the current DOM element is no longer suitable for rendering (e.g., it is removed or hidden).

Arguments:

node
HTMLElement
required

The new HTML element where the avatar should be rendered.

Example usage:

// Assume `avatar` is your SDK instance and `newContainer` is a valid HTML element
const newContainer = document.getElementById('avatar-container');
avatar.attachDOMNode(newContainer);

on()

The on() method are used to manage event subscriptions related to the avatar.

  • on(event, callback): Subscribes to an event, triggering the specified callback function each time the event occurs.

Why is this important?

The on() method allows you to track various events related to the avatar, such as the start and end of speech playback, state changes, or errors. This enables you to interact with the avatar in real time, responding to its actions efficiently.

When should you use on()?

  • Use on() to subscribe to events and respond to changes in the avatar’s behavior, such as speech completion, successfull initialization, and more.

Arguments:

event
string
required

The name of the event to subscribe or unsubscribe from.

callback
function
required

The function that will be called when the event occurs.

Example usage::

function handleInit() {
  console.log('Avatar successfully inited');
}
avatar.on('init', handleInit);
For more information about available events and their descriptions, see the events section.

off()

The off() method are used to manage event subscriptions related to the avatar.

  • off(event, callback): Unsubscribes from the event, removing the previously registered callback function.

Why is this important?

The off() method is used to unsubscribe from events when tracking them is no longer necessary, preventing unnecessary callback calls and improving performance.

When should you use off()?

  • Use off() to remove subscriptions when you no longer need to track an event.

Arguments:

event
string
required

The name of the event to subscribe or unsubscribe from.

callback
function
required

The function that will be called when the event occurs.

Example usage::

function handleInit() {
  console.log('Avatar successfully inited');
}
avatar.off('init', handleInit);
For more information about available events and their descriptions, see the events section.

dispose()

The dispose() method terminates the current session and releases all resources associated with the avatar.

Why is this important?

The dispose() method is used to cleanly end the avatar session and free up resources that were allocated for its operation. This method is essential for managing memory and ensuring that resources are properly released when the avatar is no longer needed or when you want to reset the session.

When should you use dispose()?

  • When you want to end the avatar session and ensure that all related resources are freed.
  • Before reinitializing the avatar or creating a new session to avoid potential conflicts or memory leaks.
  • When the avatar is no longer needed in your application and you want to perform a cleanup.

Example usage:

await avatar.dispose()

Return Value: The method returns a promise that resolves once the disposal process is complete.

Ensure that you call dispose() when you are finished using the avatar to maintain optimal performance and resource management.