Translation App

Transcript will appear here.

Translation will appear here.

// app.js const recordButton = document.getElementById('recordButton'); const endRecordButton = document.getElementById('endRecordButton'); const playButton = document.getElementById('playButton'); const transcriptElement = document.getElementById('transcript'); const translationElement = document.getElementById('translation'); let recognition; let speechSynthesisUtterance; let translatedText = ''; // Check if the browser supports the Web Speech API if ('webkitSpeechRecognition' in window) { recognition = new webkitSpeechRecognition(); recognition.continuous = false; recognition.interimResults = false; recognition.lang = 'auto'; recognition.onstart = function() { recordButton.disabled = true; endRecordButton.disabled = false; transcriptElement.textContent = 'Listening...'; }; recognition.onresult = function(event) { const transcript = event.results[0][0].transcript; transcriptElement.textContent = `You said: ${transcript}`; translateText(transcript); }; recognition.onerror = function(event) { console.error('Speech recognition error', event.error); recordButton.disabled = false; endRecordButton.disabled = true; transcriptElement.textContent = 'Error occurred in recognition: ' + event.error; if (event.error === 'not-allowed' || event.error === 'audio-capture') { alert('Please allow microphone access to use this feature.'); } }; recognition.onend = function() { recordButton.disabled = false; endRecordButton.disabled = true; }; } else { alert('Your browser does not support the Web Speech API'); } // Request microphone permissions navigator.mediaDevices.getUserMedia({ audio: true }) .then(function(stream) { console.log('Microphone access granted'); recordButton.addEventListener('click', () => { recognition.start(); }); endRecordButton.addEventListener('click', () => { recognition.stop(); }); }) .catch(function(err) { console.error('Microphone access denied', err); alert('Please allow microphone access to use this feature'); }); // Play button event playButton.addEventListener('click', () => { if (translatedText) { speakText(translatedText); } }); // Translate text using a translation API async function translateText(text) { const apiKey = 'AIzaSyAYu9sUAcy4CGXqtW-ygbzdrmI1HUbfJGg'; // Replace with your API key const url = `https://translation.googleapis.com/language/translate/v2?key=${apiKey}`; const response = await fetch(url, { method: 'POST', body: JSON.stringify({ q: text, target: 'en' }), headers: { 'Content-Type': 'application/json' } }); if (response.ok) { const data = await response.json(); translatedText = data.data.translations[0].translatedText; translationElement.textContent = `Translation: ${translatedText}`; playButton.disabled = false; } else { console.error('Translation API error', response.statusText); translationElement.textContent = 'Translation failed'; } } // Speak the translated text function speakText(text) { speechSynthesisUtterance = new SpeechSynthesisUtterance(text); speechSynthesisUtterance.lang = 'en-US'; window.speechSynthesis.speak(speechSynthesisUtterance); }
error: Content is protected !!
Scroll to Top