Discussions

Ask a Question
Back to All

avatar.speak in typscript sends prompt isntead of using exact text

I have my typscript here using the SDK, .speak is supposed to be default "TALK" right? Why does it sends prompt instead? What command should I send? Also it keeps saying about heygen heygen. I want it to speak my "textToSpeak" variable.

Below is my setup


  // Initialize Avatar Session
  const initializeAvatarSession = async () => {
    try {
      console.log("Initializing Avatar Session...");
      const token = await fetchAccessToken(); // Get the access token

      const avatarInstance = new StreamingAvatar({ token });

      const session = await avatarInstance.createStartAvatar({
        quality: AvatarQuality.Low,
        avatarName: "default",
        voice: {
            voiceId: "1985984feded457b9d013b4f6551ac94",
            rate: 1, // 0.5 ~ 1.5
          },
      });

      console.log("Session started:", session);

      setAvatar(avatarInstance);
      setSessionData(session);
      setIsSessionActive(true);

      avatarInstance.on(StreamingEvents.STREAM_READY, (event) => {
        if (event.detail && videoRef.current) {
          videoRef.current.srcObject = event.detail;
          videoRef.current.onloadedmetadata = () => {
            videoRef.current.play().catch(console.error);
          };
        } else {
          console.error("Stream is not available");
        }
      });

      avatarInstance.on(StreamingEvents.STREAM_DISCONNECTED, () => {
        console.log("Stream disconnected");
        if (videoRef.current) videoRef.current.srcObject = null;
        setIsSessionActive(false);
      });
    } catch (error) {
      console.error("Error initializing avatar session:", error);
    }
  };

  // Terminate Avatar Session
  const terminateAvatarSession = async () => {
    if (avatar && sessionData) {
      console.log("Terminating Avatar Session...");
      await avatar.stopAvatar();
      if (videoRef.current) videoRef.current.srcObject = null;
      setAvatar(null);
      setSessionData(null);
      setIsSessionActive(false);
    }
  };

Below is sending the text

`  useEffect(() => {
    const speakText = async () => {
      if (!avatar || !textToSpeak || !isSessionActive) {
        console.warn("Avatar is not ready or textToSpeak is empty");
        return;
      }
      try {
        console.log("2-Speaking text:", textToSpeak); // Verify the text
        await avatar.speak({ text: textToSpeak });
      } catch (error) {
        console.error("Error during avatar speak:", error);
      }
    };
  
    speakText();
  }, [textToSpeak, avatar, isSessionActive]);