What went wrong | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
24th Feb 2022, 12:32 PM
Richies
Richies - avatar
15 Answers
+ 3
You could also use a function statement instead of the arrow function to reference the desired context/object. Arrow functions don't bind this, but function statements do. https://www.freecodecamp.org/news/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26/
24th Feb 2022, 9:06 PM
ODLNT
ODLNT - avatar
+ 5
Okay thanks bro Mustafa A
25th Feb 2022, 10:01 AM
Richies
Richies - avatar
+ 4
The audio tag is missing a couple of things... - need to use https instead of http, this is causing the initial CORS problem - mixed content - also need to add crossorigin = "anonymous" to the audio tag, this will resolve the next CORS issue - MediaElementAudioSource outputs zeroes due to CORS access restrictions <audio crossorigin="anonymous" src="https://www.sololearn.com/uploads/audio.mp3"></audio>
25th Feb 2022, 9:54 PM
ODLNT
ODLNT - avatar
+ 3
data-playing
24th Feb 2022, 7:09 PM
Richies
Richies - avatar
+ 3
Mustafa A the button
24th Feb 2022, 7:24 PM
Richies
Richies - avatar
+ 2
ODLNT thanks fixed it, but it seems there is an area with the code reading the audio
24th Feb 2022, 10:24 PM
Richies
Richies - avatar
+ 1
Well the dataset is undefined. What's "this.dataset" supposed to be?
24th Feb 2022, 3:23 PM
Mustafa A
Mustafa A - avatar
+ 1
I don't think you are using "this" right. What do you think this is referencing here?
24th Feb 2022, 7:22 PM
Mustafa A
Mustafa A - avatar
+ 1
Yeah. That's a misunderstanding of how this works. This references the object context. The "addEventListener" is a function of the button. But it runs on the context it's executed. If that "this" was used inside the button object itself, that would work. But here, "this" reference the surrounding object context. Which is the global root object, "window". Not the button. You need to add "event" (or any other reference name) as parameter to the function used to handle the event. You can then reference the button object from the event that it raised, using "event.target". Like this: playButton.addEventListener("click",(event)=>{ const btn = event.target; if(audioContext.state==="suspended"){ audioContext.resume(); } if(btn.dataset.playing==='false'){ audioElement.play(); btn.dataset.playing='true'; } else if(btn.dataset.playing==='true'){ audioElement.pause(); btn.dataset.playing='false'; } },false);
24th Feb 2022, 7:54 PM
Mustafa A
Mustafa A - avatar
+ 1
Peculiar_codes That's CORS error. You are trying to use a resource that have restricted access. That's a server thing. Use something more public. Try one of these https://www.kozco.com/tech/soundtests.html
24th Feb 2022, 10:43 PM
Mustafa A
Mustafa A - avatar
+ 1
ODLNT THANKS
26th Feb 2022, 10:53 AM
Richies
Richies - avatar
+ 1
The URL you are using as the source for audio should start with https not http.
26th Feb 2022, 2:30 PM
ODLNT
ODLNT - avatar
0
On my way
26th Feb 2022, 4:40 AM
timothy rocker
0
ODLNT IT still doesn't work well
26th Feb 2022, 10:55 AM
Richies
Richies - avatar
0
Okay thanks Should I just edit the url or look for another url with the http protocol?
26th Feb 2022, 8:06 PM
Richies
Richies - avatar