# How to play and stop sound on key press + function

**URL:** <https://phaser.discourse.group/t/how-to-play-and-stop-sound-on-key-press-function/4252>\
**Category:** Phaser 3\
**Created:** [November 13, 2019, 10:13pm UTC](https://phaser.discourse.group/t/how-to-play-and-stop-sound-on-key-press-function/4252 "2019-11-13T22:13:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![okmikaelaok](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/okmikaelaok/32/2317_2.png) [@okmikaelaok](https://phaser.discourse.group/u/okmikaelaok)\
**Post date:** [November 13, 2019, 10:13pm UTC](https://phaser.discourse.group/t/how-to-play-and-stop-sound-on-key-press-function/4252/1 "2019-11-13T22:13:48Z")

</div>

Hello, I’m new to Phaser 3 and in my Options Scene if M is pressed background audio will play and if N is pressed all music is stopped. I’m currently trying to make the audio to work, I haven’t added the stop function yet.

Here’s my code and I followed someone’s tutorial, it’s detecting the key press, but the audio won’t play.

create() {  
// Add background  
this.add.sprite(800, 450, “optionsbg”);

```
// Add background audio
this.audio = this.sound.add("musicA");    

// Add M as an input key
this.input.keyboard.addKeys("M");
this.input.keyboard.on("keydown_M", function() {
  console.log("M is pressed");
  this.audio.play();
  this.audio.loop = true;
  console.log("Playing music");    
});

```

}  
} // END OF CREATE

![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/6/6088472f89bdb03d2169cbcf1a3d16d8bca6d430.png)

---

<div class="post-metadata">

**Author:** ![prob](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/prob/32/628_2.png) [@prob](https://phaser.discourse.group/u/prob)\
**Post date:** [November 14, 2019, 1:05am UTC](https://phaser.discourse.group/t/how-to-play-and-stop-sound-on-key-press-function/4252/2 "2019-11-14T01:05:26Z")

</div>

[This](https://medium.com/quick-code/understanding-the-this-keyword-in-javascript-cb76d4c7c5e8). Either bind your original scope to the keydown function, or create a reference to it outside the keydown function and use that.
