The VmonsterRoom instance provides a variety of methods to manage and control the interactions and functionalities within the virtual space.

These methods enable developers to automate tasks and enhance user engagement efficiently.

The following are the instance methods.


join()

async join(options: JoinOptions): Promise<void>

This method is used to connect to a VmonsterRoom.

If the method operates successfully, the joined event will be triggered.

Parameters

The config parameter accepts an object of type JoinOptions.

interface JoinOptions {
  sessionId: string;
  streamId: string;
  token: string;
  config?: SessionConfig;
}

Error Occurrence

  • An error will be triggered if the method is called after the joining or joined events have already occurred.

speak()

async speak(
  config?: MessageConfig
): Promise<void>

This method requests the AI Avatar to speak based on the provided text or text stream. You can receive the AI Avatar’s video stream data based on the input text. You can optionally configure the background, position, and scale of the AI Avatar for each request. It supports standard text input as well as streaming input(AsyncIterable), allowing for real-time speech generation.

Parameters

The config parameter accepts an object of type MessageConfig, which is a union of the following: TextMessageConfig and StreamMessageConfig.

// Standard text utterance
interface TextMessageConfig {
  text: string;
  isStream?: false;
  background?: File | Blob | null;
  positionX?: number | null;
  positionY?: number | null;
  scale?: number | null;
}
// Streaming text utterance
interface StreamMessageConfig {
  isStream: true;
  stream: AsyncIterable<string>;
  background?: File | Blob | null;
  positionX?: number | null;
  positionY?: number | null;
  scale?: number | null;
}

When speak() is called, the following steps occur sequentially.

  1. The AIAvatarState transitions to loading.

  2. When the AIAvatar begins speaking:

    a. The aiavatar-start-speaking event is triggered.

    b. The AIAvatarState transitions to speaking.

  3. When the AI Avatar finishes speaking:

    a. The aiavatar-stop-speaking event is triggered.

    b. The AIAvatarState transitions to idle.

  4. The AIAvatar’s speech text can be verified through the callback argument of the aiavatar-message event.

Error Occurrence

  • The RoomState is not joined when the method is called.
  • The AIAvatarState is not idle when the method is called.

stopSpeaking()

async stopSpeaking(): Promise<void>

This method is requests the AI Avatar to stop speaking

You can request to when the AI Avatar is speaking.

After the request, the speaking stops after 1 ~ 2 seconds.

  • the aiavatar-stop-speaking event is triggered.
  • The AIAvatarState transitions to idle.

Error Occurrence:

  • The AIAvatarState is not speaking when the method is called.

leave()

leave(): void

This method is used to exit the VmonsterRoom.

Upon execution, the left event is triggered immediately.

Error Occurrence

  • An error will be triggered if the method is called before the joined event has occurred.

addVideo()

addVideo(style?: Partial<CSSStyleDeclaration>): void;

This method adds an AI Avatar Video Element to the DOM.

It checks for the presence of a <video> element with the id “aiavatar-video” and adds it if it doesn’t already exist.

Parent Element Determination:

  • If an element with the id “aiavatar-video-parent” is found, the video element is added as a child of this element.
  • If no such element exists, the video element is added as a child of document.body.

Styling:

  • If a style is provided as input, the video element is added with the specified style applied.
  • If no style is provided, the element is added with the following default styles applied.
{
  width: "100%",
  height: "100%",
  objectFit: "cover",
  boxSizing: "border-box",
  aspectRatio: "calc(16 / 9)",
}

removeVideo()

removeVideo(): void

This method removes the AIAvatar Video Element from the DOM.

Note that even after the element is removed, the audio will continue to play.


unmuteUserAudio()

unmuteUserAudio(): void

Connects the user’s audio track. STT starts immediately.

muteUserAudio()

muteUserAudio(): void

This method mutes the user’s audio track.

The track remains connected, so STT can resume with minimal delay when the audio is unmuted again.

unPublishUserAudio()

unmuteUserAudio(): void

This method unpublishes the user’s audio track.

The track is disconnected, and STT can be restarted by unmuteUserAudio().


stt()

async stt(audioBlob: Blob, language: LanguageType): Promise<string>
// LanguageType : "ko" | "en" | "zh" | "ja"

This method converts a voice Blob into text.

This converts audio to text in a non-real-time manner, separately from the user’s audio track connection.

Error Occurrence

  • An error will be triggered if the method is called before the joined event has occurred.

startRecordingAudio()

startRecordingAudio(): void

This method initiates audio recording using navigator.mediaDevices.getUserMedia. Use this when calling the non-real-time stt() method.

Error Occurrence:

  • The browser does not support getUserMedia.
  • The browser does not support MediaRecorder.

stopRecordingAudio()

async stopRecordingAudio(): Promise<Blob>

This method stops the audio recording and returns the recorded audio as a Blob. Use this when calling the non-real-time stt() method.

Error Occurrence:

  • An error will be triggered if the method is called when not currently recording.

The VmonsterRoom instance provides a variety of methods to manage and control the interactions and functionalities within the virtual space.

These methods enable developers to automate tasks and enhance user engagement efficiently.

The following are the instance methods.


join()

async join(options: JoinOptions): Promise<void>

This method is used to connect to a VmonsterRoom.

If the method operates successfully, the joined event will be triggered.

Parameters

The config parameter accepts an object of type JoinOptions.

interface JoinOptions {
  sessionId: string;
  streamId: string;
  token: string;
  config?: SessionConfig;
}

Error Occurrence

  • An error will be triggered if the method is called after the joining or joined events have already occurred.

speak()

async speak(
  config?: MessageConfig
): Promise<void>

This method requests the AI Avatar to speak based on the provided text or text stream. You can receive the AI Avatar’s video stream data based on the input text. You can optionally configure the background, position, and scale of the AI Avatar for each request. It supports standard text input as well as streaming input(AsyncIterable), allowing for real-time speech generation.

Parameters

The config parameter accepts an object of type MessageConfig, which is a union of the following: TextMessageConfig and StreamMessageConfig.

// Standard text utterance
interface TextMessageConfig {
  text: string;
  isStream?: false;
  background?: File | Blob | null;
  positionX?: number | null;
  positionY?: number | null;
  scale?: number | null;
}
// Streaming text utterance
interface StreamMessageConfig {
  isStream: true;
  stream: AsyncIterable<string>;
  background?: File | Blob | null;
  positionX?: number | null;
  positionY?: number | null;
  scale?: number | null;
}

When speak() is called, the following steps occur sequentially.

  1. The AIAvatarState transitions to loading.

  2. When the AIAvatar begins speaking:

    a. The aiavatar-start-speaking event is triggered.

    b. The AIAvatarState transitions to speaking.

  3. When the AI Avatar finishes speaking:

    a. The aiavatar-stop-speaking event is triggered.

    b. The AIAvatarState transitions to idle.

  4. The AIAvatar’s speech text can be verified through the callback argument of the aiavatar-message event.

Error Occurrence

  • The RoomState is not joined when the method is called.
  • The AIAvatarState is not idle when the method is called.

stopSpeaking()

async stopSpeaking(): Promise<void>

This method is requests the AI Avatar to stop speaking

You can request to when the AI Avatar is speaking.

After the request, the speaking stops after 1 ~ 2 seconds.

  • the aiavatar-stop-speaking event is triggered.
  • The AIAvatarState transitions to idle.

Error Occurrence:

  • The AIAvatarState is not speaking when the method is called.

leave()

leave(): void

This method is used to exit the VmonsterRoom.

Upon execution, the left event is triggered immediately.

Error Occurrence

  • An error will be triggered if the method is called before the joined event has occurred.

addVideo()

addVideo(style?: Partial<CSSStyleDeclaration>): void;

This method adds an AI Avatar Video Element to the DOM.

It checks for the presence of a <video> element with the id “aiavatar-video” and adds it if it doesn’t already exist.

Parent Element Determination:

  • If an element with the id “aiavatar-video-parent” is found, the video element is added as a child of this element.
  • If no such element exists, the video element is added as a child of document.body.

Styling:

  • If a style is provided as input, the video element is added with the specified style applied.
  • If no style is provided, the element is added with the following default styles applied.
{
  width: "100%",
  height: "100%",
  objectFit: "cover",
  boxSizing: "border-box",
  aspectRatio: "calc(16 / 9)",
}

removeVideo()

removeVideo(): void

This method removes the AIAvatar Video Element from the DOM.

Note that even after the element is removed, the audio will continue to play.


unmuteUserAudio()

unmuteUserAudio(): void

Connects the user’s audio track. STT starts immediately.

muteUserAudio()

muteUserAudio(): void

This method mutes the user’s audio track.

The track remains connected, so STT can resume with minimal delay when the audio is unmuted again.

unPublishUserAudio()

unmuteUserAudio(): void

This method unpublishes the user’s audio track.

The track is disconnected, and STT can be restarted by unmuteUserAudio().


stt()

async stt(audioBlob: Blob, language: LanguageType): Promise<string>
// LanguageType : "ko" | "en" | "zh" | "ja"

This method converts a voice Blob into text.

This converts audio to text in a non-real-time manner, separately from the user’s audio track connection.

Error Occurrence

  • An error will be triggered if the method is called before the joined event has occurred.

startRecordingAudio()

startRecordingAudio(): void

This method initiates audio recording using navigator.mediaDevices.getUserMedia. Use this when calling the non-real-time stt() method.

Error Occurrence:

  • The browser does not support getUserMedia.
  • The browser does not support MediaRecorder.

stopRecordingAudio()

async stopRecordingAudio(): Promise<Blob>

This method stops the audio recording and returns the recorded audio as a Blob. Use this when calling the non-real-time stt() method.

Error Occurrence:

  • An error will be triggered if the method is called when not currently recording.