Event when new aduio file is loaded javascript
You can use the load
event on the Audio
object in JavaScript to detect when a new audio file is loaded. Here's an example:
const audio = new Audio('path/to/audiofile.mp3');
audio.addEventListener('load', () => {
console.log('Audio file loaded!');
});
This code creates a new Audio
object and attaches an event listener to the load
event. When the audio file is loaded, the event listener will be triggered and the console will log a message.
You can also use the onload
property to set the event listener:
const audio = new Audio('path/to/audiofile.mp3');
audio.onload = () => {
console.log('Audio file loaded!');
};
Note that the load
event is only triggered when the audio file is fully loaded, which means that the audio data has been decoded and is ready to be played.
If you want to detect when the audio file is partially loaded, you can use the progress
event instead:
const audio = new Audio('path/to/audiofile.mp3');
audio.addEventListener('progress', (event) => {
console.log(`Audio file loaded: ${event.loadedBytes} bytes`);
});
This event is triggered repeatedly as the audio file is loaded, and the event.loadedBytes
property contains the number of bytes that have been loaded so far.