# Pomodoro Clock audio problem

**URL:** <https://forum.freecodecamp.org/t/pomodoro-clock-audio-problem/279533>\
**Category:** I Built This\
**Created:** [May 25, 2019, 2:32am UTC](https://forum.freecodecamp.org/t/pomodoro-clock-audio-problem/279533 "2019-05-25T02:32:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![paladinvaan2018](https://avatars.discourse-cdn.com/v4/letter/p/c68b51/32.png) [@paladinvaan2018](https://forum.freecodecamp.org/u/paladinvaan2018)\
**Post date:** [May 25, 2019, 2:32am UTC](https://forum.freecodecamp.org/t/pomodoro-clock-audio-problem/279533/1 "2019-05-25T02:32:04Z")

</div>

I’m working on the pomodoro clock project functionality before I handle the CSS and I have one test that just will not pass. I have an audio file in the project that should play but instead it throws an error saying Uncaught (in promise) DOMException. I understand this is happening because of Chrome autoplay police but the audio will also not play in either Chrome or Firefox. Can anyone help?

Here is the link: [https://codepen.io/sam\_donaldson2018/pen/bZmvvP](https://codepen.io/sam_donaldson2018/pen/bZmvvP)

---

<div class="post-metadata">

**Author:** ![Dereje1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dereje1/32/63598_2.png) [@Dereje1](https://forum.freecodecamp.org/u/Dereje1)\
**Post date:** [May 25, 2019, 10:32pm UTC](https://forum.freecodecamp.org/t/pomodoro-clock-audio-problem/279533/2 "2019-05-25T22:32:08Z")

</div>

You are close, I noticed that you were trying to access your audio file with native javascript, `getElementById` and then probably with event listeners , however you don’t need to, because this is one of [the use cases of using react’s](https://reactjs.org/docs/refs-and-the-dom.html#when-to-use-refs) `ref`, here is how you do it

first add the ref attribute to your audio element  
`<audio ref={this.audio} id="beep" src={BEEP}/>`

Next bind the audio element to the `this` of your class in the constructor of the component using `React.createRef()`

`this.audio = React.createRef()`

You now have access to your audio component anywhere in the class, however, you can only access it’s attributes by using the `current` property,  
so for example, `this.audio.current.play()` would play the audio

One last thing, maybe your audio src play time is too long (I’m not sure) but it wouldn’t work with your source for me, I used my own source, [https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/success.mp3](https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/success.mp3) and was able to pass all the tests.

You also have to rewind the audio to pass the final test but I’ll let you figure that out.

---

<div class="post-metadata">

**Author:** ![paladinvaan2018](https://avatars.discourse-cdn.com/v4/letter/p/c68b51/32.png) [@paladinvaan2018](https://forum.freecodecamp.org/u/paladinvaan2018)\
**Post date:** [May 25, 2019, 11:04pm UTC](https://forum.freecodecamp.org/t/pomodoro-clock-audio-problem/279533/3 "2019-05-25T23:04:43Z")

</div>

Thanks for the help. I didn’t know about refs but the reason why it actually wasn’t working though was I had used the wrong sort of Google Drive link. Thanks to your comment on the link I realised what was wrong, changed it and now it works.

I am curious though after reading your comments and the React documentation on refs why I would use them over the strategy I was using. Are refs inherently better than native JavaScript?

---

<div class="post-metadata">

**Author:** ![Dereje1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dereje1/32/63598_2.png) [@Dereje1](https://forum.freecodecamp.org/u/Dereje1)\
**Post date:** [May 25, 2019, 11:30pm UTC](https://forum.freecodecamp.org/t/pomodoro-clock-audio-problem/279533/4 "2019-05-25T23:30:37Z")

</div>

> [@paladinvaan2018](#):
>
> I am curious though after reading your comments and the React documentation on refs why I would use them over the strategy I was using. Are refs inherently better than native JavaScript?

good question. In reality, both ways would be imperatively interacting with the real DOM and not the virtual DOM, the latter being the declarative ‘reactive’ way of doing things considering the one way data flow that react espouses, but still if you must go the imperative route, using refs is more inline with react than getting the DOM node with an id, this below seems a decent answer:

> <https://stackoverflow.com/questions/37273876/reactjs-this-refs-vs-document-getelementbyid>

---

<div class="post-metadata">

**Author:** ![paladinvaan2018](https://avatars.discourse-cdn.com/v4/letter/p/c68b51/32.png) [@paladinvaan2018](https://forum.freecodecamp.org/u/paladinvaan2018)\
**Post date:** [May 25, 2019, 11:42pm UTC](https://forum.freecodecamp.org/t/pomodoro-clock-audio-problem/279533/5 "2019-05-25T23:42:18Z")

</div>

Thanks for the link. I’ll have a read and then see about changing my approach.

I have found though that using either approach does not solve the Uncaught (in promise) DOMException error, which still occurs even though all tests now pass.

---

<div class="post-metadata">

**Author:** ![Dereje1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dereje1/32/63598_2.png) [@Dereje1](https://forum.freecodecamp.org/u/Dereje1)\
**Post date:** [May 25, 2019, 11:45pm UTC](https://forum.freecodecamp.org/t/pomodoro-clock-audio-problem/279533/6 "2019-05-25T23:45:01Z")

</div>

yeah it does for me too, the reason is audio.play() returns a promise and it’s rejection (for what ever reason) needs to be caught

---

<div class="post-metadata">

**Author:** ![paladinvaan2018](https://avatars.discourse-cdn.com/v4/letter/p/c68b51/32.png) [@paladinvaan2018](https://forum.freecodecamp.org/u/paladinvaan2018)\
**Post date:** [May 26, 2019, 2:59am UTC](https://forum.freecodecamp.org/t/pomodoro-clock-audio-problem/279533/7 "2019-05-26T02:59:21Z")

</div>

The problem seems to be that Google requires user interaction for audio to be played and because the tests do not have any the error is thrown. i have changed my code so that the promise is caught but nothing happens because otherwise it would throw the test suite off. It does not occur on Edge or Firefox so I am going to just leave it there.

---

<div class="post-metadata">

**Author:** ![mysweetr0se](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mysweetr0se/32/136766_2.png) [@mysweetr0se](https://forum.freecodecamp.org/u/mysweetr0se)\
**Post date:** [June 7, 2020, 5:00pm UTC](https://forum.freecodecamp.org/t/pomodoro-clock-audio-problem/279533/8 "2020-06-07T17:00:13Z")

</div>

A year on, this thread really helped unstuck me with the same thing. Many thanks!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 7:46pm UTC](https://forum.freecodecamp.org/t/pomodoro-clock-audio-problem/279533/9 "2021-01-17T19:46:08Z")

</div>

Closed on January 17, 2021, 7:46pm UTC


