# How to use audio in svelte

**URL:** https://forum.kirupa.com/t/how-to-use-audio-in-svelte/660595
**Category:** Uncategorized
**Created:** [August 1, 2023, 8:23pm UTC](https://forum.kirupa.com/t/how-to-use-audio-in-svelte/660595 "2023-08-01T20:23:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![davidjm](https://avatars.discourse-cdn.com/v4/letter/d/838e76/32.png) [@davidjm](https://forum.kirupa.com/u/davidjm)
#### Post date: [August 1, 2023, 8:23pm UTC](https://forum.kirupa.com/t/how-to-use-audio-in-svelte/660595/1 "2023-08-01T20:23:20Z")

</div>

Hi, How do I use audio properties such as ‘currentTime’ and ‘play’ in Svelte?  
Thanks for any help

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [August 2, 2023, 10:24pm UTC](https://forum.kirupa.com/t/how-to-use-audio-in-svelte/660595/2 "2023-08-02T22:24:20Z")

</div>

Hi @davidjm - do you have an example of your page that we can look at? JavaScript properties shouldn’t change when you are using Svelte. I am assuming you are using the audio API?

Thanks,  
Kirupa 🙂

---

<div class="post-metadata">

### Author: ![davidjm](https://avatars.discourse-cdn.com/v4/letter/d/838e76/32.png) [@davidjm](https://forum.kirupa.com/u/davidjm)
#### Post date: [August 3, 2023, 7:14am UTC](https://forum.kirupa.com/t/how-to-use-audio-in-svelte/660595/3 "2023-08-03T07:14:09Z")

</div>

Hi I’m trying to build this [js30 drumkit](https://www.discoverdev.io/blog/series/js30/01-drumkit/) using Sveltejs  
Do i need to install, import or bind the audio?  
I’m a complete newbi so I’m not clear on the concepts.

```auto
function playSound(e){
  // selects the audio element where data-key="e.keyCode"
  const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`)

  //if there is no audio element with the corresponding keyCode, then don't do anything
  if(!audio) return;

  //else reset the audio time and play it
  audio.currentTime = 0
  audio.play()
}

```

Thanks for your help

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [August 3, 2023, 12:24pm UTC](https://forum.kirupa.com/t/how-to-use-audio-in-svelte/660595/4 "2023-08-03T12:24:21Z")

</div>

Ah! Are you a newbie with Svelte or with JavaScript in general?

If it is the latter, I would suggest taking a few steps back and learning more of the fundamentals first. My free content here may be a good resource:

> **[Learn JavaScript: Visual and Beginner-Friendly Tutorials](https://www.kirupa.com/javascript/learn_javascript.htm)**
>
> Visual and beginner-friendly tutorials that teach you JavaScript starting from the basics and going all the way to advanced topics that will help you build real apps!

What you are doing in this code is getting a reference to the `audio` HTML element that is in your web page. The actual audio itself is specified in HTML where the src points to the file to play.
