-1

I am just learning about promises and would really appreciate if you could give me a simple example where you click on a button and an mp3 file plays using promises. Ive searched around and have been stuck on this the whole day.

rickyk95
  • 11
  • 6
  • I indeed did, but I keep getting the following error:"Uncaught (in promise) DOMException". – rickyk95 Aug 12 '20 at 23:33
  • questions on any forum are best to focus on a single nugget of information so folks who search for a topic can see the matching question with equally focused answers ... avoid conflating multiple stream of consciousness needs and wants AKA the promise topic which is orthogonal to ability to play audio – Scott Stensland Aug 13 '20 at 01:48
  • As I clarified post-hoc, the question was about promises insofar as they are related to playing audio. – rickyk95 Aug 13 '20 at 02:25
  • To avoid unrealistic expectations I‘d suggest this may take multiple days to grasp and research fully. I would also recommend breaking down the problem a little more. Start with a promise that will log to console, then add a gui button. At the same time, look at a simple example of playing an audio file on a button press with JavaScript https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement/Audio . Once you’re happy you have both working, glue them together. If you can’t get it to work, then you should have a nice code snippet to post here. – fdcpp Aug 13 '20 at 06:30

1 Answers1

0

Here is a quick example on how to play an mp3 file:

/********HTML***********/
<div id="audio">Click to play</div>


/********JS*********/
const div = document.getElementById('audio')

div.addEventListener('click', playAudio())

function playAudio() {
    const audio = new Audio('file.mp3')
    audio.play()
}

Though I advice you to go look for the Audio object on the documentation, because there's a lot more you can do with it, and also because searching by your self is a very better way to learn than to ask for help before making some research.

  • Thanks. I guess I didn't explain myself well. Contrary to what you assumed, I have been browsing trying to understand promises for a few hours now. I was looking for an example of making a button play music with promises as opposed to event listeners. – rickyk95 Aug 12 '20 at 23:42