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

Page Visibility API page plays audio when switching tabs without having pressed play #26745

Closed
zcorpan opened this issue May 12, 2023 · 0 comments · Fixed by #26748 or #33177
Closed

Page Visibility API page plays audio when switching tabs without having pressed play #26745

zcorpan opened this issue May 12, 2023 · 0 comments · Fixed by #26748 or #33177
Labels
Content:WebAPI Web API docs needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened.

Comments

@zcorpan
Copy link
Contributor

zcorpan commented May 12, 2023

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API

What specific section or headline is this issue about?

No response

What information was incorrect, unhelpful, or incomplete?

Open the page, switch to another tab and switch back. It starts playing audio, which is unexpected.

What did you expect to see?

No audio unless I've pressed 'play' first.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

Use the play event to start listening to page visibility changes:

const audio = document.querySelector("audio");
// When the audio starts playing...
audio.addEventListener("play", () => {
  // Handle page visibility change:
  // - If the page is hidden, pause the video
  // - If the page is shown, play the video
  document.addEventListener("visibilitychange", () => {
    if (document.hidden) {
      audio.pause();
    } else {
      audio.play();
    }
  });
}, {once: true});

MDN metadata

Page report details
@zcorpan zcorpan added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label May 12, 2023
@github-actions github-actions bot added the Content:WebAPI Web API docs label May 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened.
Projects
None yet
1 participant