My voice assistant website won't work on mobile

31 Views Asked by At

I have been trying to make a voice assistant with the web speech api

it works fine on pc but it has very bad hearing on mobile

i have to say "hey hub" wait 3 seconds and say my command and even then half the time it never hears. me

here is my speech recognition code:

let listeningForCommand = false;
let lastTranscript = '';
const wakeWord = "hey hub";
const silenceDelay = 2000; // 2 seconds
let silenceTimer;
let messages = [];
const maxMessages = 7; // Maximum number of messages to keep


window.onload = function() {
    const savedMessages = localStorage.getItem('messages');
    if (savedMessages) {
        messages = JSON.parse(savedMessages);
      startRecognition()
       loadModels();
        initShakeDetection()
    }
    setGif('https://i.ibb.co/WxbwbD1/Normal-gif.gif'); // Set default GIF
    updateDateTime(); // Initialize date and time

    // Speak a greeting message
    responsiveVoice.speak("oh. im back! ", "Australian Male", {
        onstart: function() {
            // You can set a specific GIF for the greeting here if you like
            setGif('https://i.ibb.co/Lp3XTHs/Smiling-eyes-gif.gif');
        },
        onend: function() {
            setGif('https://i.ibb.co/WxbwbD1/Normal-gif.gif');
        }
    });
};

let recognition;
if ('SpeechRecognition' in window) {
    recognition = new SpeechRecognition();
} else if ('webkitSpeechRecognition' in window) {
    recognition = new webkitSpeechRecognition();
} else {
    console.error('Your browser does not support SpeechRecognition.');
}

recognition.lang = 'en-US'; // Set language
recognition.continuous = true;
recognition.interimResults = true;

recognition.onresult = function(event) {
    clearTimeout(silenceTimer);

    const currentTranscript = Array.from(event.results)
                                   .map(result => result[0].transcript)
                                   .join('');

    if (!listeningForCommand && currentTranscript.toLowerCase().includes(wakeWord)) {
        listeningForCommand = true;
        lastTranscript = '';
        setGif('https://i.ibb.co/fn40PJm/I-hear-you.gif');
        addSystemMessage();
    } else if (listeningForCommand) {
        lastTranscript = currentTranscript;
        silenceTimer = setTimeout(() => {
            const commandAfterWakeWord = lastTranscript.substring(lastTranscript.toLowerCase().indexOf(wakeWord) + wakeWord.length).trim();
            if (commandAfterWakeWord) {
                setGif('https://i.ibb.co/B2JmjJc/Thinking.gif');
                sendCommandToAPI(commandAfterWakeWord);
            } else {
                setGif('https://i.ibb.co/x1XZHHm/Huh.gif');
            }
            listeningForCommand = false;
            recognition.stop(); // Stop recognition while processing the command
        }, silenceDelay);
    }
};

recognition.onerror = function(event) {
    if (event.error === 'no-speech') {
        console.error('No speech detected.');
        restartRecognition();
    }
};

function restartRecognition() {
    if (recognition) {
        recognition.stop();
        setTimeout(() => {
            startRecognition();
        }, 1000);
    }
}

recognition.start();

I tried on both pc and mobile on 4 deferent devices

it works fine on pc but it has very bad hearing on mobile

i have to say "hey hub" wait 3 seconds and say my command and even then half the time it never hears. me

I hope someone here can help fix this.

0

There are 0 best solutions below