cover-img

playing

16 March, 2023

0

0

0

About

The playing event is triggered when an audio or video element starts playing. This event is useful for developers who want to add functionality or perform certain actions while a media file is playing, such as displaying a progress bar, updating the current time, or creating animations that sync with the audio or video.

It is important to note that the playing event may not be supported by all browsers, so you should always test your code across different browsers and versions to ensure it works as expected. Additionally, since the playing event is triggered repeatedly while the media is playing, it is important to optimise your code for performance to prevent lag or other issues. Overall, the playing event is a powerful tool for developers looking to add interactivity and dynamic functionality to their audio and video content.

Event listener

Here's an example of using addEventListener() to listen for the playing event on an HTML audio element:

HTML

<audio id="myAudio" src="my-audio-file.mp3"></audio>

JavaScript

const myAudio = document.getElementById("myAudio");

myAudio.addEventListener("playing", () => {
console.log("Media playback has started and is no longer buffering.");
});

Here, we use the document.getElementById() method to get a reference to the HTML audio element with an ID of "myAudio". We then register a playing event listener on the myAudio element using addEventListener(). When the media playback starts playing and is no longer buffering, the callback function specified in the event listener is executed, which logs a message to the console.

The playing event can be useful for performing actions when the media playback has started and is no longer buffering, such as updating the UI or displaying a notification to the user.

Property

Here's an example of using the onplaying property to assign a function to be executed when the playing event is fired on an HTML audio element:

const myAudio = document.getElementById("myAudio");

myAudio.onplaying = () => {
console.log("Media playback has started and is no longer buffering.");
};

Here, we use the document.getElementById() method to get a reference to the HTML audio element with an ID of "myAudio". We then assign a function to the onplaying property of the myAudio element, which will be executed when the playing event is fired.

Inline

Here's an example of using the onplaying property inline to assign a function to be executed when the playing event is fired on an HTML audio element:

<audio id="myAudio" src="my-audio-file.mp3" onplaying="console.log('Media playback has started and is no longer buffering.')"></audio>

Here, we use the onplaying property inline in the HTML code to assign a function that logs a message to the console when the playing event is fired on the myAudio element.

Note that using inline event handlers can make your code harder to maintain and debug, especially when working with more complex applications. It is generally recommended to use the addEventListener() method to register event listeners in your JavaScript code instead of inline event handlers.

Programmatic trigger

There is no playing() method in JavaScript for programmatic triggering of the playing event. Instead, you can use the dispatchEvent() method to create and dispatch a playing event on a media element:

const myAudio = document.getElementById("myAudio");

const playingEvent = new Event("playing");
myAudio.dispatchEvent(playingEvent);

Here, we use the document.getElementById() method to get a reference to the HTML audio element with an ID of "myAudio". We then create a new playing event using the Event() constructor and assign it to the playingEvent variable. Finally, we use the dispatchEvent() method to dispatch the playing event on the myAudio element.

When the playing event is dispatched, any event listeners registered for the playing event on the myAudio element will be executed, or the function assigned to the onplaying property will be called if one is present.

Note that programmatically triggering events should be used sparingly, and only when necessary. In most cases, events are triggered by user interaction or other natural triggers, so manually triggering events may not be required in typical use cases.

javascript

0

0

0

javascript

Milsaware
C#, PHP, Javascript, Kotlin App Developer

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.