JavaScript's new Audio() method cannot play audio. How to solve it?
Apr 05, 2025 pm 08:57 PM Solution to failed audio playback in JavaScript new Audio()
method
When playing audio using JavaScript's new Audio()
method, you often encounter the problem of failed playback. This article will analyze a common case and provide effective solutions.
In the case, the developer used new Audio()
to create an audio object, but the console reported an error uncaught (in promise) DOMException: Failed to load because no supported source was found.
, and the audio cannot be played.
The error message indicates that the browser cannot load the audio file. Combined with the code example (the HTML and JS code shown in the image are omitted here), the problem is mainly the asynchronousness of audio loading and browser security policies.
new Audio(url)
method loads the audio resource asynchronously. After the new Audio()
statement is executed, the audio is not loaded immediately, and calling music.play()
directly may cause playback failure.
Workaround: Take advantage of canplaythrough
event. This event is triggered when the audio resource can be played smoothly. Listen to the canplaythrough
event, make sure that the audio has been loaded successfully, and then call play()
method:
const music = new Audio('./1.mp3'); music.addEventListener("canplaythrough", event => { music.play(); });
This code creates an audio object and adds a canplaythrough
event listener. When the audio is loaded and playable, the listener triggers and calls music.play()
.
If it still fails to play, it may be that the browser security policy restricts automatic playback. In order to improve user experience, some browsers restrict the page from automatically playing audio without user interaction. It is recommended to add buttons to let users trigger playback manually:
const playButton = document.getElementById('playButton'); playButton.addEventListener('click', () => { music.play(); });
If the user actively triggers playback, the browser will not block the audio playback, and ensures that the audio is played successfully while respecting the user experience.
The above is the detailed content of JavaScript's new Audio() method cannot play audio. How to solve it?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

As the internationally leading blockchain digital asset trading platform, Binance provides users with a safe and convenient trading experience. Its official app integrates multiple core functions such as market viewing, asset management, currency trading and fiat currency trading.

Toncoin (TON) is a decentralized first-tier blockchain originally conceived by the Telegram team. It is known for its high performance, low cost and user-friendly features, and aims to provide an open network platform for billions of users around the world. Its native token TON is used in the network to pay transaction fees, pledge and participate in network governance.

UsethetagtodisplayamessageorredirectuserswhenJavaScriptisdisabled.2.ApplygracefuldegradationbybuildingcorefunctionalitywithoutJavaScriptandenhancingitwhenavailable.3.Adda"no-js"classtotheHTMLelementanduseJavaScripttoreplaceitwith"js&qu

Binance is a world-renowned digital asset trading platform, providing users with secure, stable and rich cryptocurrency trading services. Its app is simple to design and powerful, supporting a variety of transaction types and asset management tools.

OKX is a world-renowned comprehensive digital asset service platform, providing users with diversified products and services including spot, contracts, options, etc. With its smooth operation experience and powerful function integration, its official APP has become a common tool for many digital asset users.

Huobi is a world-renowned digital asset service platform, providing users with a wide range of digital asset trading and management services. With its professional service, rich transaction pairs and reliable security system, it has won the trust of many users.

Binance is one of the world's well-known digital asset trading platforms, providing users with safe, stable and convenient cryptocurrency trading services. Through the Binance App, you can view market conditions, buy, sell and asset management anytime, anywhere.

Toncoin (TON) is a native cryptocurrency of The Open Network, attracting much attention in the digital currency field for its high transaction processing speed and scalability.
