Jump to content

Audio problem


Arhtur_Barker
 Share

Recommended Posts

Hi! 

So I am new to coding and I am just trying to make a small project for myself. All it is a button that when you press it, it plays randomly one of three sounds. Something is going wrong though and I can't figure out what, anyone know? 

 

<!doctype <!DOCTYPE html>
<link rel="stylesheet" href="C:\Users\Owner\Google Drive\Code\Experiments\HTML-CSS-JAVASCRIPT\Random Sound\Code\Random Sounds.css">


<h1> Random Sounds </h1>

<button id="press"> Press me! </button>


<script>
var num = 0

function pressed() {
  num = Math.floor(1 + 3 * Math.random())
  if (num == 1) {
    var audio1 = new Audio('C:\Users\Owner\Google Drive\Code\Experiments\HTML-CSS-JAVASCRIPT\Random Sound\Sounds\OFFICIAL Somewhere over the Rainbow - Israel IZ Kamakawiwoʻole.mp3');
    console.log('1')
    audio1.play()
  } else if (num == 2) {
    var audio2 = new Audio('C:\Users\Owner\Google Drive\Code\Experiments\HTML-CSS-JAVASCRIPT\Random Sound\Sounds\OFFICIAL Somewhere over the Rainbow - Israel IZ Kamakawiwoʻole.mp3');
    console.log('2')
    audio2.play()
  } else {
    var audio3 = new Audio('C:\Users\Owner\Google Drive\Code\Experiments\HTML-CSS-JAVASCRIPT\Random Sound\Sounds\OFFICIAL Somewhere over the Rainbow - Israel IZ Kamakawiwoʻole.mp3');
    console.log('3')
    audio3.play()
  }
}
press.onclick = pressed


</script>

 

 

cheers 

 

A

Link to comment
Share on other sites

Create an account or sign in to comment

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

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...