Events & Listeners Basics - Storm JS Player Core
Learn how to use event-listener system inbuilt into Storm JS Player Core for direct communication with your code.
Attaching an Event Listener
The concept of attaching events is the same as in standard JavaScript.
const storm = stormPlayerCore(config);
storm.addEventListener("playerReady", onPlayerReady);
The API for all JavaScript bundle formats (IIFE, UMD, AMD etc.) is the same. The example above uses IIFE format.
In this example, we have added a new event listener (called playerReady) to the player. Whenever the player is ready, an attached function will be called. The function can also be of an inline type.
Now we need to define our function.
function onPlayerReady(event) {
console.log(event.ref); // reference to the player object
};
We can also use inline functions.
storm.addEventListener("playerReady", function(event){
console.log(event.ref); // reference to the player object
});
Detaching an Event Listener
In order to detach (remove) an event listener just use:
storm.removeEventListener("playerReady", onPlayerReady);
For obvious reasons, inline functions cannot be removed (unless stored as a variable), however, there is a way to remove all events of specific type:
storm.removeEventListener("playerReady");
We can also remove all event listeners from our player element:
storm.removeAllEventListeners();
For the next step please check our Storm JS Player Core - Events where you'll learn about available events for player behaviour.