Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why player.volume(val) fires volumechange event? #8744

Closed
morskoyzmey opened this issue May 21, 2024 · 3 comments
Closed

Why player.volume(val) fires volumechange event? #8744

morskoyzmey opened this issue May 21, 2024 · 3 comments

Comments

@morskoyzmey
Copy link

morskoyzmey commented May 21, 2024

Example. Once user changes volume in one player I want to set that volume for all the rest players on the page.
I'm doing this in volumechange event, where for each player (except current) I call p.volume(new_val). But each such call fires another volumechange event. So we get the recursion here, which breaks only because there is no event for unchanged value.

That behaviour I barely met in any API I've used before.

Video.js 8.6.1

Copy link

welcome bot commented May 21, 2024

👋 Thanks for opening your first issue here! 👋

If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.
To help make it easier for us to investigate your issue, please follow the contributing guidelines.

@amtins
Copy link
Contributor

amtins commented May 22, 2024

@morskoyzmey This behavior is specific to the mediaElement. The recursion should stop by itself after a few executions, because if the same value is passed to the mediaElement, it is not supposed to emit a new volumenchange event. But it is possible to do something like:

videojs.getAllPlayers().forEach((player) => {
  player.on('volumechange', () => {
    const volume = player.volume();

    videojs.getAllPlayers().forEach((playerToUpdate) => {
      if (player === playerToUpdate || playerToUpdate.volume() === volume) return;

      playerToUpdate.volume(volume);
    });
  });
});

@mister-ben
Copy link
Contributor

That behaviour I barely met in any API I've used before.

This is the same behaviour of a plain video element. volumechange is triggered when you set <video>.volume to a new value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants