Drum machine sound not working

why doesn’t my audio work? the Q button should be setup to make cow noises

So a few issues here:

  1. You need to bind functions to a stateful class component, or else this.playSound does not work. Use:
this.methodName=this.methodName.bind(this);
  1. .getElementsById('audioQ')[0] does not work because it is getElementById since id is unique, and consequently the index [0] should also be removed

  2. in the <button>, it should be `id=‘audioQ’ with lowercase i.

  3. in the <button>, it should be onClick={this.playSound} with uppercase C.

  4. the src while points to a valid mp3, does not play. When I used one of the sounds used in the sample drum machine it does: https://s3.amazonaws.com/freecodecamp/drums/Chord_3.mp3.

Hi there,

There are a few problems with your code:
The used event in the button must be onClick and not onclick.
In addition, document.getElementsById is not a function. You probably meant document.getElementById which returns only one element by unique id, so you don’t need to index it anymore.
Solving these two will set you on track and your sound will play.

Hope that helps.

all good now! thanks so much guys! more to come, no doubt

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.