This zone is sleeping right now! ZzzZZzzz

Better not disturb it! ~You go to the petrol station and buy a slushy instead, everything is cool!~

This area will open again in 000.beats!

(Learn about Swatch Time)

Some other zones are awake :^]


Artifacts Gallery Guilds Search Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining?
a Summer day - @546.24
Activity rating: Three Stars Posts & Arts: 34/1k.beats Random | Recent Posts | Guild Recents
News: :happy: Open the all windows! Your mind needs storms and air! :happy: Guild Events: Summer Projects

+  MelonLand Forum
|-+  Life & The Web
| |-+  ✁ ∙ Web Crafting
| | |-+  ☔︎ ∙ I need Help!
| | | |-+  HELP: (Javascript) How do I make audio play when clicking an image?


« previous next »
Pages: [1] Print Embed
Author Topic: HELP: (Javascript) How do I make audio play when clicking an image?  (Read 4939 times)
OneMillionFurries
Sr. Member ⚓︎
****
View Profile WWW


Gettin silly with it
⛺︎ My Room

Artifacts:
specimenThanks for being rad!First 1000 Members!Joined 2023!
« on: a Summer night » Embed

As the title says. I am trying to make an audio clip play when you click an image on my page, but I have never before used javascript so I am completely lost.

I am using the code from here but I am not actually sure what I'm doing. Here's what I've edited it to be:

Javascript
Code
    <!-- Baldhead Voice code start -->
    <script type="text/javascript">
      function playAudio(home/Baldhead Voices/1.mp3) {
        new Audio(home/Baldhead Voices/1.mp3).play();
        }
    </script>

HTML
Code
<img src="/graphics/sitegraphics/faust-winsmall2.gif"width="auto" height="250" onclick="playAudio('home/Baldhead Voices/1.mp3')">

Any help at all would be hugely appreciated!

P.S. if anyone could help me figure out how to play one of multiple voice lines randomly on click that would be SUPER awesome! Thank you in advance! :4u:

Edit: title edited to include "(Javascript)"

« Last Edit: a Summer night by OneMillionFurries » Logged

https://onemillionfurries.com/graphics/misc/bh%20ad.gif
https://onemillionfurries.com/graphics/misc/Robokyskateexplode.gif
akesi
Jr. Member ⚓︎
**
View Profile WWW


⛺︎ My Room

Artifacts:
First 1000 Members!Joined 2023!
« Reply #1 on: a Summer night » Embed

so your issue is that you've replaced the variable with plain text, which confuses javascript. you'd want to do the following:

Code
    
<img src="/graphics/sitegraphics/faust-winsmall2.gif" width="auto" height="250" onclick="playAudio('home/Baldhead Voices/1.mp3')">

...

<!-- Baldhead Voice code start -->
<script type="text/javascript">
function playAudio(audioClip) {
    new Audio(audioClip).play();
}
</script>

brief explainer: "playAudio" is what programmers call a function. essentially you give functions inputs, and then they do something (in this case, it plays an audio clip). the inputs are variables. so you are setting the variable "audioClip" to have the value 'home/Baldhead Voices/1.mp3' when you set the function to activate when the onclick event happens

for playing a random sound, you could make an list, and each element in the list contains the path to each audio clip. then whenever you call the function, you can randomly choose one of the audio clips to play, like the following:

Code
<img src="/graphics/sitegraphics/faust-winsmall2.gif" width="auto" height="250" onclick="playAudio()">

<!-- Baldhead Voice code start -->
<script type="text/javascript">
const audioClipPaths = ["path/to/sound1.wav", "path/to/sound2.wav", "path/to/sound3.wav"]; 

function playAudio() {
  let index = Math.floor(Math.random() * 3);
  let audioClip = audioClipPaths[index]

  new Audio(audioClip).play();
}
</script>


so you should be able to use the above, just change the "path/to/sound1.wav" -- and keep the quotation marks! let me know if anything is unclear. excited to see what sorts of sounds a bald head makes

Logged
OneMillionFurries
Sr. Member ⚓︎
****
View Profile WWW


Gettin silly with it
⛺︎ My Room

Artifacts:
specimenThanks for being rad!First 1000 Members!Joined 2023!
« Reply #2 on: a Summer night » Embed

so you should be able to use the above, just change the "path/to/sound1.wav" -- and keep the quotation marks! let me know if anything is unclear. excited to see what sorts of sounds a bald head makes

OMGGG it works!! tysmm! :D :transport: If ya wanna know what sorts of sounds a bald head makes, look for this guy on my website's homepage and do a clicky >:]
https://onemillionfurries.com/graphics/sitegraphics/faust-winsmall2.gif

Logged

https://onemillionfurries.com/graphics/misc/bh%20ad.gif
https://onemillionfurries.com/graphics/misc/Robokyskateexplode.gif
Pages: [1] Print Embed 
« previous next »
 

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021 | Privacy Notice | Send Feedback | Supporters ♥ Forum Guide | Rules | RSS | WAP | Mobile


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi
@000 MelonLand Zap! Want to Login or Join ? Forum Art Hub Chat Webring Editor Recent Edits Tenement Arcade Web Guides Graphics Catalogue Wiki Newsletters Image Stream
Melon's Sites TamaNotchi Textures PixelSea GifyPet MoMG Ozwomp Online Loom Videos Leaky Webring Melonking
Tools Melon Software ArtHub Embed Maker ML Passports
Outlinks Webrings Internet Phone Book HTML Energy Declarations Hackers & Designers Frutiger Aero Forum m15o's Web Services 32Bit Cafe iMood
Minecraft: Online
Join: craft.melonking.net