Sound board in HTML, CSS and Js

Sound board in HTML, CSS and Js

Introduction

A sound board is an interface that lets users play audio clips by clicking buttons or pressing keys. They can play funny quotes, music samples, or sound effects instantly.

soundboard

Tools and Technologies Used

  • CSS (Cascading Style Sheets): To style and design the layout visually appealing.
  • Js(JavaScript): To add interactivity and handle sound playback.
  • Audio Files: Short MP3 or WAV clips for the sound board.
  • Code-editors: Vs code, Sublime code, Notepad++.
  • Browsers: Google chrome, Mozilla Firefox.

Project Setup

soundboard-project/

├── index.html
├── style.css
└── script.js


Step-by-Step Implementation

  • HTML Layout
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">     
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w==" crossorigin="anonymous" />
    <link rel="stylesheet" href="style.css">
    <title>Sound board</title>
</head>
<body>
    <audio id = "Beep" src="sounds/beep.mp3"></audio>
    <audio id = "Bird" src="sounds/bird.mp3"></audio>
    <audio id = "Jungle" src="sounds/jungle.mp3"></audio>
    <audio id = "Car" src="sounds/car.mp3"></audio>
    <audio id = "Winning" src="sounds/winning.mp3"></audio>
    <audio id = "Firework" src="sounds/firework.mp3"></audio>

    <div id="buttons"></div>

    <script src="script.js"></script>
</body>
</html>


  • CSS Styling
@import url('https://fonts.googleapis.com/css?Muli&display=swap');

* {
    box-sizing: border-box;
}

body {
    background-color: rgb(150, 200, 200); 
    font-family: 'Muli', sans-serif;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    text-align: center;
    height: 100vh; 
    overflow: hidden;
    margin: 0;
}

.btn {
    background-color: rgb(17, 150, 143);
    border-radius: 5px;
    border: none;
    color: #fff;
    margin: 1rem;
    padding: 1.5rem 3rem;
    font-size: 1.2rem;
    font-family: inherit;
    cursor: pointer;
}

.btn:hover {
    opacity: 0.9;
}

.btn:focus {
    outline: none;

}

  • JavaScript Functionality
 const sounds = ['Beep', 'Bird', 'Jungle', 'Car', 'Winning', 'Firework']
 
 sounds.forEach(sound => {
     const btn = document.createElement('button')
     btn.classList.add('btn')

     btn.innerText = sound

     btn.addEventListener('click', () => {
         stopSong()
         document.getElementById(sound).play()
     })

     document.getElementById('buttons').appendChild(btn)
 })

 function stopSong() {
     sounds.forEach(sound => {
         const song = document.getElementById(sound)

         song.pause()
         song.currentTime = 0;
     })
 }

Common Mistakes and Debugging Tips

While making a sound board you might be mistaken while writing the code. Make sure you check twice the code. A simple dot, comma, closing tags may will leads you to some error.

Audio not playingCheck the audio path and file extension
Multiple sounds overlappingUse a single Audio object or pause previous
Buttons not workingVerify data-sound matches file names
Sounds not loading in browserEnsure files are inside the same project folder


Q1. Can I use different sound formats like WAV or OGG?
Yes, modern browsers support multiple audio formats. You can use .mp3, .wav, or .ogg, but ensure consistency across all files.


Q2. How do I host my sound board online?
You can host it for free on GitHub Pages, Netlify, or Vercel. Simply upload your project files and deploy.


Q3. Can I make a musical instrument sound board?
Absolutely. Replace the audio clips with instrument samples like piano or drums for a playable music pad.


Q4. How to make my sound board mobile-friendly?
Use responsive CSS and media queries. Make buttons larger and touch-friendly.


Q5. Can I add background music?
Yes, but ensure it doesn’t interfere with the soundboard sounds. You can use a looping background audio file controlled by separate buttons.

Leave a Reply

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