There’s nothing wrong with your code. Modern browsers disable autoplay by default unless certain criteria are met. In general, one of the following will allow autoplay:
- The user clicks on the page*
- The media does not have audio or the audio is muted by default
- The user has explicitly whitelisted the page to autoplay media with audio
- The browser enables autoplay after detecting the user interacts with the page frequently
*Generally only one action is allowed per click. Actions include opening a new window, navigating, autoplaying media, etc…