Monday, April 7, 2025

Html coding for adding a mute/play button if you want better user control (and to not annoy visitors 😅) in blogger

 Absolutely! Let's give your Blogger background music a mute/play button so users can control it — and you stay in their good books 😄


✅ HTML + JavaScript Code for Background Music with Mute/Play Button

Paste this into a HTML/JavaScript gadget or directly into your Blogger theme where you'd like the button to appear:

<!-- Background Music Player -->

<audio id="bg-music" autoplay loop>

  <source src="YOUR_AUDIO_FILE_URL.mp3" type="audio/mpeg">

  Your browser does not support the audio element.

</audio>


<!-- Mute/Play Button -->

<button id="music-toggle" style="position:fixed; bottom:20px; right:20px; z-index:9999; padding:10px 15px; font-size:16px;">

  🔊 Music On

</button>


<script>

  const music = document.getElementById('bg-music');

  const toggleBtn = document.getElementById('music-toggle');


  // Check if audio is playing

  function isPlaying(audio) {

    return !audio.paused;

  }


  toggleBtn.addEventListener('click', () => {

    if (isPlaying(music)) {

      music.pause();

      toggleBtn.textContent = '🔇 Music Off';

    } else {

      music.play();

      toggleBtn.textContent = '🔊 Music On';

    }

  });

</script>

  • You can also add hidden if you don’t want the player to be visible:

<audio autoplay loop hidden> <source src="your-audio-file.mp3" type="audio/mpeg"> </audio>

🔧 What to Do:

  1. Replace YOUR_AUDIO_FILE_URL.mp3 with your direct mp3 link.

  2. The button will appear fixed at the bottom-right of your page.

  3. Visitors can toggle the music on/off easily.


💡 Optional Styling Tip:

You can change the button’s look with CSS or replace it with an icon or image for a fancier feel.

Want me to help with a custom-styled button or icon version too?

No comments:

Post a Comment

Html Coding for Blinking Text in Blog

 Sure! Here's a simple way to make text blink in an HTML blog using CSS (since the old <blink> tag is deprecated and not support...