Jump to content

Sound Effect on a button click

Go to solution Solved by DesignerLeo,

Recommended Posts

Can someone help me (I've been a web designer with SS for over two years, this is my first audio request):

Please help -- I've been trying to figure this out all week with no luck.

 **How can I have an audio file play when a button is clicked?

I want to use an audio file (I already have the /s/ audio file loaded on my page) when anyone clicks a button on the website. What code should I add? Do I only need CSS, or will I need code injections, or embedded block?

I've tried using the embed block with this code--the button works, but the sound does not. Is my code wrong or do I need to try a different method to get the sound file to play on a button click?

<audio id="myAudio">
  <source src="/s/Pickleball-Hit-2.mp3" type="audio/mpeg">
</audio>
<!-- link button -->
<center> <a href="/contact"><button onclick="playAudio(/s/Pickleball-Hit-2.mp3)" type="button">
  CONTACT US</button></a></center>
  
  <style>
  button {
  padding 21px 34px;
  font-family: "Sofia Pro" Sofia, Pro, sans-serif;
  font-size: 20px;
  font-weight: 500;
  letter-spacing: 1px;
  line-height: 2.6em;
  font-family: "Sofia Pro" Sofia, Pro, sans-serif;
  font-style: normal;
  text-transofrm: uppercase;
  color: #ffffff;
  background-color: #19d826;
  border-color: #fff;
  }
  </style>

 

website: https://slicepb.squarespace.com

pass: pb

 

Image 8-23-24 at 9.51 AM.jpeg

Edited by jblpropel
Figured out one problem, but still missing sound
Link to comment

Try this as a start for uploading and more:

 

Please like and upvote if my comments were helpful to you. Cheers!

Zygmunt Spray
Squarespace Website Designer
Contact me: 
https://squarefortytwo.com 
Hire me on Upwork!

🔌 Ghost Squarespace Plugins (Referral link)
📈 SEO Space (Referral link)
 SquareWebsites Plugins (Referral link)
 🔲 SQSP Themes (Referral link) 
Spark Plugin (Referral link) 
 🖼️ Gallery Lightbox Plugin (Referral link) 

 Did I help? Buy me a coffee?

Link to comment

I added the code to the home page button (in the first section under the the logo--see pic).

I want all button clicks to trigger it (if there is code to address all button clicks), but if that's not doable, I'm okay with changing every button block to an embedded code block (that looks like a button)--similar to the "Contact Us" button under the logo--if it makes the sound work 

Image 8-26-24 at 8.44 AM.jpeg

Link to comment

@tuanphan I removed the embedded block and replaced it with the standard button block.  I found code that you uploaded on elfsight (see below). I added it to the page header and updated the blockID. This is the first time that the sound actually worked on a button click -- BUT the button function stopped working (directing to the button's page link of /contact). What can I add to this code to make the button function work again (taking the visitor to the Contact Page AND keep the sound working).

Here is the code:

<audio id="player" src="/s/PickleballHit2.mp3"> </audio>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<script>
jQuery(document).ready(function() {
  var playing = false;
  jQuery("#block-yui_3_17_2_1_1724704962175_6435").click(function() {
    if (playing == false) {
      document.getElementById("player").play();
      playing = true;
    } else {
      document.getElementById("player").pause();
      playing = false;
    }
  });
});
</script>

Link to comment
  • Solution
Posted (edited)

you could try something like this in the header code injection:

 

<!--SIMPLE ALL BUTTONS PLAY A NOSIE ON CLICK-->
<script>
document.addEventListener('DOMContentLoaded', function() { //make sure document is loaded

let buttons = document.querySelectorAll(".sqs-block-button-container"); //get all buttons, add or change if your buttons have different classes
var BTNaudioCLICK = new Audio('INSERT LINK TO AUDIO FILE HERE'); //provide a link to your audio file

BTNaudioCLICK.preload = 'auto'; //make sure the audio is ready
BTNaudioCLICK.volume = 1; //set volume to 100%

function CLICK(event) {
BTNaudioCLICK.pause(); //pause to avoid clipping sounds on spam
BTNaudioCLICK.playbackRate = 1; //set speed of audio playback
BTNaudioCLICK.currentTime = 0; //set audio time to beginning
BTNaudioCLICK.play(); //play the sound
}

for(const button of buttons) {
button.addEventListener('mousedown', CLICK, false); //assign click listeners to all buttons
}

}); //closing the dom loaded statement
</script>

I am unsure if I closed all the brackets correctly but chatGPT should help you along nicely with this.
This should keep your links intact.

Edited by DesignerLeo
Link to comment

@DesignerLeo Thank you so much for trying to help! I added this to the header, and it sounds better than the code I mentioned earlier (again, thank you!!).

For anyone else trying to do this -- I realized that when I try to click the button in editor mode, the sound will not play when the button has a link attached to it. However, if I go into incognito mode and try the button, the sound AND the button function correctly! 

Leo, thank you so much for helping me solve a two-week-long nightmare lol

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.