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

fix: AudioPlayerComposable - Prevent loop in firefox #935

Merged
merged 3 commits into from
Sep 17, 2024

Conversation

lhaggar
Copy link
Member

@lhaggar lhaggar commented Sep 17, 2024

Renamed branch, original PR: #929

closes #921

Currently the AudioPlayerComposable component has a problem with live streaming, where it infinitely loops the first second of audio if rendered in the Firefox browser. The component seems to work fine in Chrome. The reason it doesn't work in Firefox comes down to how the two browsers handle the onDurationChange event, or more specifically the value that is applied to duration for unbounded streams.

In Chrome the browser assigns duration the value of Infinity, which is in accordance with the HTML spec (see also MDN), but in Firefox (for reasons I haven't been able to discover) the duration property is a continuously updated float.

Since we tie this continuous update to our component's duration state and we have that same duration listed as a dependency in a useEffect, this means that the callback for the useEffect is being continuously updated for unbounded streams in Firefox. That same callback is used to initialise the component by setting the current time to 0.

  useEffect(() => {
    if (audioRef && audioRef.current && duration) {
      audioRef.current.currentTime = initialTime;
    }
    setCurrentTime(0);
    setDisplayDuration(0);
  }, [src, initialTime, duration]);

This results in the infinite loop we are seeing where the audio's current time is constantly being reset as new audio data arrives and the value of duration changes.

Since we only really appear to want to call this function initially or when the src of the audio changes, I am proposing that we resolve the issue by removing duration as a factor of the component's initial setup entirely.

⚠️ There is an additional issue for Firefox that this does not solve, which is that autoplay still doesn't work.

I have done:

  • Written unit tests against changes
  • Written functional tests against the component and/or NewsKit site
  • Updated relevant documentation

I have tested manually:

  • The feature's functionality is working as expected on Chrome, Firefox, Safari and Edge
  • The screen reader reads and flows through the elements as expected.
  • There are no new errors in the browser console coming from this PR.
  • When visual test is not added, it renders correctly on different browsers and mobile viewports (Safari, Firefox, small mobile viewport, tablet)
  • The Playground feature is working as expected

@lhaggar lhaggar requested a review from a team as a code owner September 17, 2024 08:15
@github-actions github-actions bot added the fix This change fixes a bug label Sep 17, 2024
@lhaggar lhaggar merged commit 15a5bf9 into main Sep 17, 2024
33 of 37 checks passed
@lhaggar lhaggar deleted the fix/audio-player-composable-loops-in-firefox branch September 17, 2024 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix This change fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Streaming loop error in AudioPlayerComposable
4 participants