简体   繁体   中英

How to fix audio.play() is not a function

I can't see why it doesn't work.It seems all fine

const button = document.querySelector('.play');
const alarm = document.getElementById('alarm');



button.addEventListener('click',function(){
   alarm.play();
})

this is the html :

        <audio id="alarm">
            <source src="sound/Wecker-sound.mp3">
        </audio>

Not a better answer. This is for when I search SO in a year: I got the 'audio.play() is not a function' error because I was accidentally creating a malformed object that didn't have the right properties (stupidity with renaming props being passed in React*), so JS choked trying to process the command. As soon as I fixed that and provided the proper file source, error went away.

*Original array of audio files had this.state keys like 'url' and 'id', but when i iterated over the array, i did dumb crap like , but then when building the actual based on that, I was still trying to use 'url' and 'id' instead of the newly renamed props 'clip' and 'clipId'. I'm rambling, but it took me way too long to get this stuff worked out, and figure out my errors (and near as I can tell, I can probably get rid of a layer of props passing... refactoring to follow once its all up and running).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM