Drum machine plays the same sound





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















My drum machine plays the same sounds regardless of the button pressed, all the links are different, when I looked at the console.log output it shows that this.audio is the last url in my keys object. Can someone explain why?



const keys = [{
name: 'Q',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3',
description: 'Heater-4_1',
},{
name: 'W',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3',
description: 'Heater-1',
},{
name: 'E',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3',
description: 'Kick_n_Hat',
},{
name: 'A',
url: 'https://s3.amazonaws.com/freecodecamp/drums/punchy_kick_1.mp3',
description: 'Chord_1',
},{
name: 'S',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Chord_1.mp3',
description: 'punchy_kick_1',
},{
name: 'D',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3',
description: 'Brk_Snr',
},{
name: 'Z',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Dry_Ohh.mp3',
description: 'Dry_Ohh',
},{
name: 'X',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3',
description: 'Heater-3',
},{
name: 'C',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3',
description: 'Dsc_Oh',
},
];

class App extends React.Component{
constructor(props){
super(props);

this.state = {
play:false,
};

this.handleClick = this.handleClick.bind(this);
}

handleClick() {
console.log(this.audio);

this.setState({
play:!this.state.play,
});

this.audio.play();
this.audio.currentTime = 0;
}

render(){
let drumpad = keys.map(item => <div id={item.description}>
<div id='display'>
<button className='drum-pad' id={item.name} onClick={this.handleClick}>{item.name}
<audio className='clip' id={item.name} src={item.url} ref={ref => this.audio = ref}/>
</button>
</div>
</div>);

return (
<div id='drum-machine'>
{drumpad}
</div>
);
}
}

ReactDOM.render(<App />, document.getElementById('root'));









share|improve this question































    -1















    My drum machine plays the same sounds regardless of the button pressed, all the links are different, when I looked at the console.log output it shows that this.audio is the last url in my keys object. Can someone explain why?



    const keys = [{
    name: 'Q',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3',
    description: 'Heater-4_1',
    },{
    name: 'W',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3',
    description: 'Heater-1',
    },{
    name: 'E',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3',
    description: 'Kick_n_Hat',
    },{
    name: 'A',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/punchy_kick_1.mp3',
    description: 'Chord_1',
    },{
    name: 'S',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Chord_1.mp3',
    description: 'punchy_kick_1',
    },{
    name: 'D',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3',
    description: 'Brk_Snr',
    },{
    name: 'Z',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Dry_Ohh.mp3',
    description: 'Dry_Ohh',
    },{
    name: 'X',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3',
    description: 'Heater-3',
    },{
    name: 'C',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3',
    description: 'Dsc_Oh',
    },
    ];

    class App extends React.Component{
    constructor(props){
    super(props);

    this.state = {
    play:false,
    };

    this.handleClick = this.handleClick.bind(this);
    }

    handleClick() {
    console.log(this.audio);

    this.setState({
    play:!this.state.play,
    });

    this.audio.play();
    this.audio.currentTime = 0;
    }

    render(){
    let drumpad = keys.map(item => <div id={item.description}>
    <div id='display'>
    <button className='drum-pad' id={item.name} onClick={this.handleClick}>{item.name}
    <audio className='clip' id={item.name} src={item.url} ref={ref => this.audio = ref}/>
    </button>
    </div>
    </div>);

    return (
    <div id='drum-machine'>
    {drumpad}
    </div>
    );
    }
    }

    ReactDOM.render(<App />, document.getElementById('root'));









    share|improve this question



























      -1












      -1








      -1








      My drum machine plays the same sounds regardless of the button pressed, all the links are different, when I looked at the console.log output it shows that this.audio is the last url in my keys object. Can someone explain why?



      const keys = [{
      name: 'Q',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3',
      description: 'Heater-4_1',
      },{
      name: 'W',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3',
      description: 'Heater-1',
      },{
      name: 'E',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3',
      description: 'Kick_n_Hat',
      },{
      name: 'A',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/punchy_kick_1.mp3',
      description: 'Chord_1',
      },{
      name: 'S',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Chord_1.mp3',
      description: 'punchy_kick_1',
      },{
      name: 'D',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3',
      description: 'Brk_Snr',
      },{
      name: 'Z',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Dry_Ohh.mp3',
      description: 'Dry_Ohh',
      },{
      name: 'X',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3',
      description: 'Heater-3',
      },{
      name: 'C',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3',
      description: 'Dsc_Oh',
      },
      ];

      class App extends React.Component{
      constructor(props){
      super(props);

      this.state = {
      play:false,
      };

      this.handleClick = this.handleClick.bind(this);
      }

      handleClick() {
      console.log(this.audio);

      this.setState({
      play:!this.state.play,
      });

      this.audio.play();
      this.audio.currentTime = 0;
      }

      render(){
      let drumpad = keys.map(item => <div id={item.description}>
      <div id='display'>
      <button className='drum-pad' id={item.name} onClick={this.handleClick}>{item.name}
      <audio className='clip' id={item.name} src={item.url} ref={ref => this.audio = ref}/>
      </button>
      </div>
      </div>);

      return (
      <div id='drum-machine'>
      {drumpad}
      </div>
      );
      }
      }

      ReactDOM.render(<App />, document.getElementById('root'));









      share|improve this question
















      My drum machine plays the same sounds regardless of the button pressed, all the links are different, when I looked at the console.log output it shows that this.audio is the last url in my keys object. Can someone explain why?



      const keys = [{
      name: 'Q',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3',
      description: 'Heater-4_1',
      },{
      name: 'W',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3',
      description: 'Heater-1',
      },{
      name: 'E',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3',
      description: 'Kick_n_Hat',
      },{
      name: 'A',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/punchy_kick_1.mp3',
      description: 'Chord_1',
      },{
      name: 'S',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Chord_1.mp3',
      description: 'punchy_kick_1',
      },{
      name: 'D',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3',
      description: 'Brk_Snr',
      },{
      name: 'Z',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Dry_Ohh.mp3',
      description: 'Dry_Ohh',
      },{
      name: 'X',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3',
      description: 'Heater-3',
      },{
      name: 'C',
      url: 'https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3',
      description: 'Dsc_Oh',
      },
      ];

      class App extends React.Component{
      constructor(props){
      super(props);

      this.state = {
      play:false,
      };

      this.handleClick = this.handleClick.bind(this);
      }

      handleClick() {
      console.log(this.audio);

      this.setState({
      play:!this.state.play,
      });

      this.audio.play();
      this.audio.currentTime = 0;
      }

      render(){
      let drumpad = keys.map(item => <div id={item.description}>
      <div id='display'>
      <button className='drum-pad' id={item.name} onClick={this.handleClick}>{item.name}
      <audio className='clip' id={item.name} src={item.url} ref={ref => this.audio = ref}/>
      </button>
      </div>
      </div>);

      return (
      <div id='drum-machine'>
      {drumpad}
      </div>
      );
      }
      }

      ReactDOM.render(<App />, document.getElementById('root'));






      reactjs audio






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 4:35









      FrankerZ

      17.9k73067




      17.9k73067










      asked Nov 17 '18 at 0:52









      Jrobm2k9Jrobm2k9

      286




      286
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You overwrite this.audio every single time as your keys.map() iterates through the array. I'd avoid using this.audio all together. Simply use the button onClick event. Pass event to the function, and use console.log(event.target.querySelector('audio').play());:



          class App extends React.Component{
          constructor(props){
          super(props);

          this.state = {
          play:false,
          };

          this.handleClick = this.handleClick.bind(this);
          }

          handleClick(event) {
          this.setState({
          play:!this.state.play,
          });

          const audio = event.target.querySelector('audio');

          audio.play();
          audio.currentTime = 0;
          }

          render(){
          let drumpad = keys.map(item => <div id={item.description}>
          <div id='display'>
          <button className='drum-pad' id={item.name} onClick={this.handleClick}>{item.name}
          <audio className='clip' id={item.name} src={item.url} />
          </button>
          </div>
          </div>);

          return (
          <div id='drum-machine'>
          {drumpad}
          </div>
          );
          }
          }

          ReactDOM.render(<App />, document.getElementById('root'));





          share|improve this answer
























          • Thanks FrankerZ, it worked, one last question, how can I target the audio if the user pressed the letter associated with the sound? I'm using the componentdidmount and componentwillunmount but i'm not sure how to target the correct sound within the function, any ideas?

            – Jrobm2k9
            Nov 17 '18 at 4:34











          • Ah, that's a bit more difficult. You'll likely want to use ref again, but this time assign it like an object: this.audio[item.name] = ref'. In your componentDidMount, add the document event listeners, and when something get's pressed, call this.audio[keyThatWasPressed] to fetch the audio for that.

            – FrankerZ
            Nov 17 '18 at 4:37












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347201%2fdrum-machine-plays-the-same-sound%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You overwrite this.audio every single time as your keys.map() iterates through the array. I'd avoid using this.audio all together. Simply use the button onClick event. Pass event to the function, and use console.log(event.target.querySelector('audio').play());:



          class App extends React.Component{
          constructor(props){
          super(props);

          this.state = {
          play:false,
          };

          this.handleClick = this.handleClick.bind(this);
          }

          handleClick(event) {
          this.setState({
          play:!this.state.play,
          });

          const audio = event.target.querySelector('audio');

          audio.play();
          audio.currentTime = 0;
          }

          render(){
          let drumpad = keys.map(item => <div id={item.description}>
          <div id='display'>
          <button className='drum-pad' id={item.name} onClick={this.handleClick}>{item.name}
          <audio className='clip' id={item.name} src={item.url} />
          </button>
          </div>
          </div>);

          return (
          <div id='drum-machine'>
          {drumpad}
          </div>
          );
          }
          }

          ReactDOM.render(<App />, document.getElementById('root'));





          share|improve this answer
























          • Thanks FrankerZ, it worked, one last question, how can I target the audio if the user pressed the letter associated with the sound? I'm using the componentdidmount and componentwillunmount but i'm not sure how to target the correct sound within the function, any ideas?

            – Jrobm2k9
            Nov 17 '18 at 4:34











          • Ah, that's a bit more difficult. You'll likely want to use ref again, but this time assign it like an object: this.audio[item.name] = ref'. In your componentDidMount, add the document event listeners, and when something get's pressed, call this.audio[keyThatWasPressed] to fetch the audio for that.

            – FrankerZ
            Nov 17 '18 at 4:37
















          1














          You overwrite this.audio every single time as your keys.map() iterates through the array. I'd avoid using this.audio all together. Simply use the button onClick event. Pass event to the function, and use console.log(event.target.querySelector('audio').play());:



          class App extends React.Component{
          constructor(props){
          super(props);

          this.state = {
          play:false,
          };

          this.handleClick = this.handleClick.bind(this);
          }

          handleClick(event) {
          this.setState({
          play:!this.state.play,
          });

          const audio = event.target.querySelector('audio');

          audio.play();
          audio.currentTime = 0;
          }

          render(){
          let drumpad = keys.map(item => <div id={item.description}>
          <div id='display'>
          <button className='drum-pad' id={item.name} onClick={this.handleClick}>{item.name}
          <audio className='clip' id={item.name} src={item.url} />
          </button>
          </div>
          </div>);

          return (
          <div id='drum-machine'>
          {drumpad}
          </div>
          );
          }
          }

          ReactDOM.render(<App />, document.getElementById('root'));





          share|improve this answer
























          • Thanks FrankerZ, it worked, one last question, how can I target the audio if the user pressed the letter associated with the sound? I'm using the componentdidmount and componentwillunmount but i'm not sure how to target the correct sound within the function, any ideas?

            – Jrobm2k9
            Nov 17 '18 at 4:34











          • Ah, that's a bit more difficult. You'll likely want to use ref again, but this time assign it like an object: this.audio[item.name] = ref'. In your componentDidMount, add the document event listeners, and when something get's pressed, call this.audio[keyThatWasPressed] to fetch the audio for that.

            – FrankerZ
            Nov 17 '18 at 4:37














          1












          1








          1







          You overwrite this.audio every single time as your keys.map() iterates through the array. I'd avoid using this.audio all together. Simply use the button onClick event. Pass event to the function, and use console.log(event.target.querySelector('audio').play());:



          class App extends React.Component{
          constructor(props){
          super(props);

          this.state = {
          play:false,
          };

          this.handleClick = this.handleClick.bind(this);
          }

          handleClick(event) {
          this.setState({
          play:!this.state.play,
          });

          const audio = event.target.querySelector('audio');

          audio.play();
          audio.currentTime = 0;
          }

          render(){
          let drumpad = keys.map(item => <div id={item.description}>
          <div id='display'>
          <button className='drum-pad' id={item.name} onClick={this.handleClick}>{item.name}
          <audio className='clip' id={item.name} src={item.url} />
          </button>
          </div>
          </div>);

          return (
          <div id='drum-machine'>
          {drumpad}
          </div>
          );
          }
          }

          ReactDOM.render(<App />, document.getElementById('root'));





          share|improve this answer













          You overwrite this.audio every single time as your keys.map() iterates through the array. I'd avoid using this.audio all together. Simply use the button onClick event. Pass event to the function, and use console.log(event.target.querySelector('audio').play());:



          class App extends React.Component{
          constructor(props){
          super(props);

          this.state = {
          play:false,
          };

          this.handleClick = this.handleClick.bind(this);
          }

          handleClick(event) {
          this.setState({
          play:!this.state.play,
          });

          const audio = event.target.querySelector('audio');

          audio.play();
          audio.currentTime = 0;
          }

          render(){
          let drumpad = keys.map(item => <div id={item.description}>
          <div id='display'>
          <button className='drum-pad' id={item.name} onClick={this.handleClick}>{item.name}
          <audio className='clip' id={item.name} src={item.url} />
          </button>
          </div>
          </div>);

          return (
          <div id='drum-machine'>
          {drumpad}
          </div>
          );
          }
          }

          ReactDOM.render(<App />, document.getElementById('root'));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 17 '18 at 1:21









          FrankerZFrankerZ

          17.9k73067




          17.9k73067













          • Thanks FrankerZ, it worked, one last question, how can I target the audio if the user pressed the letter associated with the sound? I'm using the componentdidmount and componentwillunmount but i'm not sure how to target the correct sound within the function, any ideas?

            – Jrobm2k9
            Nov 17 '18 at 4:34











          • Ah, that's a bit more difficult. You'll likely want to use ref again, but this time assign it like an object: this.audio[item.name] = ref'. In your componentDidMount, add the document event listeners, and when something get's pressed, call this.audio[keyThatWasPressed] to fetch the audio for that.

            – FrankerZ
            Nov 17 '18 at 4:37



















          • Thanks FrankerZ, it worked, one last question, how can I target the audio if the user pressed the letter associated with the sound? I'm using the componentdidmount and componentwillunmount but i'm not sure how to target the correct sound within the function, any ideas?

            – Jrobm2k9
            Nov 17 '18 at 4:34











          • Ah, that's a bit more difficult. You'll likely want to use ref again, but this time assign it like an object: this.audio[item.name] = ref'. In your componentDidMount, add the document event listeners, and when something get's pressed, call this.audio[keyThatWasPressed] to fetch the audio for that.

            – FrankerZ
            Nov 17 '18 at 4:37

















          Thanks FrankerZ, it worked, one last question, how can I target the audio if the user pressed the letter associated with the sound? I'm using the componentdidmount and componentwillunmount but i'm not sure how to target the correct sound within the function, any ideas?

          – Jrobm2k9
          Nov 17 '18 at 4:34





          Thanks FrankerZ, it worked, one last question, how can I target the audio if the user pressed the letter associated with the sound? I'm using the componentdidmount and componentwillunmount but i'm not sure how to target the correct sound within the function, any ideas?

          – Jrobm2k9
          Nov 17 '18 at 4:34













          Ah, that's a bit more difficult. You'll likely want to use ref again, but this time assign it like an object: this.audio[item.name] = ref'. In your componentDidMount, add the document event listeners, and when something get's pressed, call this.audio[keyThatWasPressed] to fetch the audio for that.

          – FrankerZ
          Nov 17 '18 at 4:37





          Ah, that's a bit more difficult. You'll likely want to use ref again, but this time assign it like an object: this.audio[item.name] = ref'. In your componentDidMount, add the document event listeners, and when something get's pressed, call this.audio[keyThatWasPressed] to fetch the audio for that.

          – FrankerZ
          Nov 17 '18 at 4:37




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347201%2fdrum-machine-plays-the-same-sound%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly