Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Automobiles (12)
    • Cars (7)
  • Blog (103)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (51)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones
Close

Search

ProgrammingJavaScript

Building a Voice-Controlled YouTube Video Player with JavaScript

By Admin
January 4, 2025 4 Min Read
0

In today’s fast-paced digital world, voice assistants have become an integral part of our everyday lives, powering devices, applications, and even websites. With advancements in web technologies, creating a voice-enabled application is no longer a daunting task. In this article, we’ll explore an innovative project: a voice-controlled YouTube video player built using JavaScript.

Developing Your Own Chat with Stranger System Using PHP

The Concept Behind the Project

The idea is simple yet powerful: allow users to control YouTube video playback using voice commands. Whether you’re hands-free at your desk or showcasing an interactive demo, this project demonstrates how voice recognition and APIs can deliver a seamless multimedia experience.

Follow this video for complete guidance :

https://www.youtube.com/watch?v=IUNN1X1h-TU

Users can issue commands like this : “Play [Video Name]“: Search and play a YouTube video.

This project leverages the Web Speech API for voice recognition and YouTube Data API for fetching video content dynamically.

Full Source Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Voice Assistant</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #1a1a2e, #16213e);
            color: #fff;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .main-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            padding: 20px;
            gap: 20px;
        }

        .video-container {
            width: 100%;
            max-width: 800px;
            aspect-ratio: 16/9;
            position: relative;
            margin-bottom: 40px;
        }

        .video-container iframe {
            width: 100%;
            height: 100%;
            border: 1px solid #fff;
            border-radius: 12px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
        }

        .mic-button {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            background: #4CAF50;
            border: none;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
        }

        .mic-button:hover {
            transform: scale(1.1);
        }

        .mic-button.listening {
            background: #f44336;
        }

        .status {
            font-size: 18px;
            color: #fff;
            text-align: center;
            margin-top: 20px;
            min-height: 27px;
            padding: 10px 20px;
            border-radius: 20px;
            background: rgba(0, 0, 0, 0.3);
            backdrop-filter: blur(10px);
        }
    </style>
</head>
<body>
    <div class="main-container">
        <div class="video-container">
            <iframe id="player" src="" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
        </div>

        <button id="micButton" class="mic-button">
            <svg class="mic-icon" viewBox="0 0 24 24" width="40" height="40" fill="white">
                <path d="M12 14c1.66 0 3-1.34 3-3V5c0-1.66-1.34-3-3-3S9 3.34 9 5v6c0 1.66 1.34 3 3 3zm5.91-3c-.49 0-.9.36-.98.85C16.52 14.2 14.47 16 12 16s-4.52-1.8-4.93-4.15c-.08-.49-.49-.85-.98-.85-.61 0-1.09.54-1 1.14.49 3 2.89 5.35 5.91 5.78V20c0 .55.45 1 1 1s1-.45 1-1v-2.08c3.02-.43 5.42-2.78 5.91-5.78.1-.6-.39-1.14-1-1.14z"/>
            </svg>
        </button>

        <div id="status" class="status"></div>
    </div>

    <script>
        const YOUTUBE_API_KEY = 'YOUR_API_KEY';

        let recognition;
        let isListening = false;
        let synthesis = window.speechSynthesis;

        function speak(text) {
            synthesis.cancel();
            const utterance = new SpeechSynthesisUtterance(text);
            synthesis.speak(utterance);
        }

        async function playSong(song) {
            updateStatus(`Searching for: ${song}`);
            speak("Searching");
            try {
                const response = await fetch(
                    `https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&q=${encodeURIComponent(song)}&key=${YOUTUBE_API_KEY}`
                );
                const data = await response.json();

                if (data.items.length > 0) {
                    const videoId = data.items[0].id.videoId;
                    const embedURL = `https://www.youtube.com/embed/${videoId}?autoplay=1`;
                    document.getElementById('player').src = embedURL;
                    updateStatus(`Now playing: ${data.items[0].snippet.title}`);
                    speak('Now Playing');
                } else {
                    speak("Couldn't find the song on YouTube.");
                    updateStatus("Song not found");
                }
            } catch (error) {
                console.error('Error playing song:', error);
                updateStatus("Error playing song");
            }
        }

        function updateStatus(message) {
            document.getElementById('status').textContent = message;
        }

        function toggleListening() {
            if (!recognition) {
                recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
                recognition.continuous = true;

                recognition.onstart = function () {
                    isListening = true;
                    document.getElementById('micButton').classList.add('listening');
                    updateStatus("Listening...");
                };

                recognition.onend = function () {
                    isListening = false;
                    document.getElementById('micButton').classList.remove('listening');
                    updateStatus("Listening stopped");
                };

                recognition.onerror = function (event) {
                    console.error('Speech recognition error:', event.error);
                    updateStatus("Error: " + event.error);
                };

                recognition.onresult = function (event) {
                    var current = event.resultIndex;
                    var transcript = event.results[current][0].transcript.trim().toLowerCase();

                    if (transcript.startsWith('play')) {
                        transcript = transcript.replace('play', '').trim();
                        playSong(transcript);
                    }
                };
            }

            if (!isListening) {
                recognition.start();
            } else {
                recognition.stop();
            }
        }

        document.getElementById('micButton').addEventListener('click', toggleListening);
    </script>
</body>
</html>

Technologies Used

  • JavaScript: The core scripting language to handle logic, voice commands, and API integration.
  • Web Speech API: Enables speech recognition and converts voice commands into actionable text.
  • YouTube IFrame API: Embeds and controls YouTube videos dynamically.
  • HTML & CSS: Structures and styles the interface for a modern, user-friendly experience.

How It Works

At the heart of this application lies the integration of voice commands with JavaScript. The user clicks a microphone button, activating the voice recognition service. Commands are processed in real-time, with specific triggers like “Play” directing the system’s actions.

When a user says “Play [song name]“, the application fetches the video ID from YouTube’s search API and dynamically loads it into the embedded player.

User Interface

The UI consists of:

  • A YouTube Player Frame: Displays and controls the video.
  • A Microphone Button: Activates or deactivates voice recognition.
  • A Status Display: Provides updates about the current command or status.

The design focuses on clarity, with an elegant microphone button that changes appearance when active, giving visual feedback to the user.

Challenges and Solutions

  • Speech Accuracy: Voice recognition is sensitive to accents and background noise. Ensuring clear enunciation improves results.
  • API Limits: YouTube API has usage quotas, so optimized queries prevent unnecessary API calls.
  • Real-Time Feedback: Immediate visual and auditory cues ensure users are always aware of system status.

Real-World Applications

  • Interactive Presentations: Control video demos without a remote.
  • Accessibility Tools: Assist users with limited mobility.
  • Hands-Free Multimedia Centers: Build kiosks or smart home hubs.

Future Improvements

While the current implementation is robust, there’s always room for enhancement:

  • Multiple Voice Commands: Add volume control or video skipping.
  • Improved Search Algorithms: Display multiple search results for user selection.
  • Custom Voice Responses: Make the assistant more interactive with dynamic speech feedback.

This voice-enabled YouTube player isn’t just a coding exercise; it’s a glimpse into the future of human-computer interaction. Combining the power of APIs with JavaScript opens doors to countless possibilities. Whether you’re a hobbyist, developer, or educator, this project is an excellent way to explore the capabilities of modern web technologies.

Start experimenting, refine the experience, and who knows—you might just build the next big voice-controlled web application!

Tags:

voice controlledyoutube
Author

Admin

Follow Me
Other Articles
Previous

Building Your Own Chatbot with Gemini API: A Step-by-Step Guide

Next

Image Size Compression Tool using JavaScript

No Comment! Be the first one.

Leave a Reply

Your email address will not be published. Required fields are marked *

FIFA World Cup 2026 Predict and Win by SportsGuff

Recent Posts

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation
  • How to Design a Student Marksheet Using HTML and CSS

Tags

adsense ai animate animation animation using HTML and CSS API blog calculator chatgpt Cryptocurrency CSS css animation design Email Facebook featured filemanager file manager free template google htaccess HTML image Instagram interview javascript JQuery jquery ui NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App PWA QR random react reactjs Rotate travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You Admin
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide Admin
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme