Update Script_Speech_recognition/videoplayer.js

This commit is contained in:
Mário Panenko 2024-11-20 22:45:32 +00:00
parent 50cc803e28
commit 830ff00b2b

View File

@ -1,27 +1,37 @@
import { createClient } from 'https://cdn.jsdelivr.net/npm/@supabase/supabase-js/+esm'; document.addEventListener('DOMContentLoaded', function () {
const supabaseUrl = 'https://manesldshonpegglmyan.supabase.co'
const supabaseKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im1hbmVzbGRzaG9ucGVnZ2xteWFuIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTczMDcxNzQ2NCwiZXhwIjoyMDQ2MjkzNDY0fQ.saaJeTtwYZCS3YKJYmQsAOSAEFzUVUJnoJSD8-lgLHo'
const supabase = createClient(supabaseUrl, supabaseKey)
// Select DOM elements
const inputText = document.getElementById('inputText'); const inputText = document.getElementById('inputText');
const videoPlayer = document.getElementById('translationVideo'); const videoPlayer = document.getElementById('translationVideo');
const translateWordsBtn = document.getElementById('translateWordsBtn'); const translateBtn = document.getElementById('translateBtn');
const translateLettersBtn = document.getElementById('translateLettersBtn'); const wordTab = document.getElementById('wordTab');
const letterTab = document.getElementById('letterTab');
const prevBtn = document.getElementById('prevBtn'); const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn'); const nextBtn = document.getElementById('nextBtn');
const translateBtn = document.getElementById('translateBtn'); // Prelož button const notification = document.getElementById('notification');
const statusText = document.getElementById('statusText'); const currentDisplay = document.getElementById('currentDisplay'); // Display for current word/letter
// Translation state variables
let videoSequence = []; let videoSequence = [];
let currentIndex = 0; let currentIndex = 0;
let autoPlayEnabled = true; let currentMode = 'letters'; // Default mode
let currentTranslationMode = 'words'; // Default mode
// Function to check if video exists (for local testing) // Switch to "words" mode
wordTab.addEventListener('click', () => {
currentMode = 'words';
wordTab.classList.add('active');
letterTab.classList.remove('active');
notification.style.display = 'none'; // Hide notification when switching mode
});
// Switch to "letters" mode
letterTab.addEventListener('click', () => {
currentMode = 'letters';
letterTab.classList.add('active');
wordTab.classList.remove('active');
notification.style.display = 'none'; // Hide notification when switching mode
});
// Function to check if a video exists
async function videoExists(src) { async function videoExists(src) {
return new Promise(resolve => { return new Promise((resolve) => {
const video = document.createElement('video'); const video = document.createElement('video');
video.src = src; video.src = src;
video.onloadeddata = () => resolve(true); video.onloadeddata = () => resolve(true);
@ -29,149 +39,80 @@ async function videoExists(src) {
}); });
} }
// Function to fetch video URL from Supabase by label (for word translation) // Generate video sequence and check for missing videos
async function fetchVideoUrl(word) { async function generateVideoSequence() {
const { data, error } = await supabase const text = inputText.value.trim();
.from('Videa') const items = currentMode === 'words' ? text.split(' ') : text.split('');
.select('video_url')
.eq('label', word)
.maybeSingle(); // Use maybeSingle() instead of single()
if (error) {
console.error('Error fetching video:', error);
return null;
}
if (!data) {
console.warn(`No video found for word: ${word}`);
return null;
}
return data.video_url;
}
// Function to load video sequence by words using Supabase
async function loadVideoSequenceByWords() {
const words = inputText.value.trim().split(' ');
videoSequence = []; videoSequence = [];
let missingVideos = false;
for (const word of words) { for (const item of items) {
const videoUrl = await fetchVideoUrl(word.toLowerCase()); const videoPath =
if (videoUrl) { currentMode === 'words'
videoSequence.push(videoUrl); ? `video/${item.toLowerCase()}.mp4`
: `video/pismena/${item.toLowerCase()}.mp4`;
const exists = await videoExists(videoPath);
if (exists) {
videoSequence.push(videoPath);
} else {
missingVideos = true;
} }
} }
currentIndex = 0;
}
async function fetchLetterVideoUrl(letter) { if (missingVideos) {
const { data, error } = await supabase notification.style.display = 'flex'; // Show notification if videos are missing
.from('Videa') } else {
.select('video_url') notification.style.display = 'none'; // Hide notification if all videos exist
.eq('label', letter)
.maybeSingle(); // Use maybeSingle() for letters
if (error) {
console.error('Error fetching video for letter:', letter, error);
return null;
} }
if (!data) { currentIndex = 0; // Reset to the beginning of the sequence
console.warn(`No video found for letter: ${letter}`);
return null;
} }
return data.video_url; // Play video at the current index
} function playVideo() {
if (videoSequence.length === 0) return;
// Function to load video sequence by letters using Supabase videoPlayer.src = videoSequence[currentIndex];
async function loadVideoSequenceByLetters() {
const characters = inputText.value.trim().split('');
videoSequence = [];
for (const char of characters) {
if (/[a-zA-Z]/.test(char)) {
const videoUrl = await fetchLetterVideoUrl(char.toLowerCase());
if (videoUrl) {
videoSequence.push(videoUrl);
}
}
}
currentIndex = 0;
}
// Function to play video at a specific index
function playVideoAtIndex(index) {
if (index < 0 || index >= videoSequence.length) return;
videoPlayer.src = videoSequence[index];
videoPlayer.play(); videoPlayer.play();
currentIndex = index;
// Update the title with the current word or letter // Update current word/letter display
const currentLabel = videoSequence[index].split('/').pop().split('.')[0]; const currentItem =
document.getElementById('currentLetter').innerText = currentLabel.toUpperCase(); currentMode === 'words'
? inputText.value.trim().split(' ')[currentIndex]
: inputText.value.trim().split('')[currentIndex];
currentDisplay.textContent = currentItem.toUpperCase();
}
if (autoPlayEnabled) { // Automatically play the next video in the sequence
videoPlayer.onended = () => { videoPlayer.addEventListener('ended', () => {
if (currentIndex < videoSequence.length - 1) {
currentIndex++; currentIndex++;
if (currentIndex < videoSequence.length) { playVideo();
playVideoAtIndex(currentIndex);
} else {
autoPlayEnabled = false;
}
};
} else {
videoPlayer.onended = null;
}
}
// Event listener for "Prelož" button (Translate button)
translateBtn.addEventListener('click', async () => {
autoPlayEnabled = true;
// Load the video sequence based on the current translation mode
if (currentTranslationMode === 'words') {
await loadVideoSequenceByWords();
} else if (currentTranslationMode === 'letters') {
await loadVideoSequenceByLetters();
}
if (videoSequence.length > 0) {
playVideoAtIndex(currentIndex);
} else {
statusText.innerText = 'No videos found for the entered text.';
} }
}); });
// Event listener for "Preložiť po slovách" button // Navigate to the previous video
translateWordsBtn.addEventListener('click', () => {
currentTranslationMode = 'words'; // Set to words mode
translateWordsBtn.classList.add('active');
translateLettersBtn.classList.remove('active');
});
// Event listener for "Preložiť po písmenách" button
translateLettersBtn.addEventListener('click', () => {
currentTranslationMode = 'letters'; // Set to letters mode
translateLettersBtn.classList.add('active');
translateWordsBtn.classList.remove('active');
});
// Event listeners for prev and next buttons to navigate video sequence
prevBtn.addEventListener('click', () => { prevBtn.addEventListener('click', () => {
if (currentIndex > 0) { if (currentIndex > 0) {
autoPlayEnabled = false;
currentIndex--; currentIndex--;
playVideoAtIndex(currentIndex); playVideo();
} }
}); });
// Navigate to the next video
nextBtn.addEventListener('click', () => { nextBtn.addEventListener('click', () => {
if (currentIndex < videoSequence.length - 1) { if (currentIndex < videoSequence.length - 1) {
autoPlayEnabled = false;
currentIndex++; currentIndex++;
playVideoAtIndex(currentIndex); playVideo();
} }
}); });
// Handle "Prelož" button click
translateBtn.addEventListener('click', async () => {
await generateVideoSequence();
if (videoSequence.length > 0) {
playVideo();
}
});
});