Bug 244667 - Volume of an Audio object cannot be changed when AudioContext is created
Summary: Volume of an Audio object cannot be changed when AudioContext is created
Status: REOPENED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Audio (show other bugs)
Version: Safari 15
Hardware: Mac (Apple Silicon) macOS 12
: P2 Major
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-09-01 07:36 PDT by aerdogan07
Modified: 2022-09-28 14:00 PDT (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description aerdogan07 2022-09-01 07:36:19 PDT
Reproduction Steps:
- Create an audio object
- Create AudioContext with createMediaElementSource for the audio object before it's played
- Play the audio
- Adjust the audio's volume programmatically

Expected:
The level of volume changes accordingly.

Observed:
The level of volume doesn't change.


Demo: https://codesandbox.io/s/ecstatic-tom-8tz5em

Code to reproduce the issue:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
  </head>
  <body>
    <button id="audio-control">Play Audio</button>
  </body>
  <script type="application/javascript">
    const audio = new Audio(
      "https://upload.wikimedia.org/wikipedia/commons/b/bb/Test_ogg_mp3_48kbps.wav"
    );
    audio.crossOrigin = "anonymous";
    audio.loop = true;
    audio.volume = 0.5;
    const audioContext = new AudioContext();

    const button = document.querySelector("#audio-control");
    button.addEventListener("click", () => {
      if (audio.paused) {
        button.innerText = "Change Volume Level";
        const audioSource = audioContext.createMediaElementSource(audio);
        const audioDestination = audioContext.createMediaStreamDestination();
        audioSource.connect(audioContext.destination);
        audioSource.connect(audioDestination);
        audio.play();
        return;
      }

      let newVolumeLevel = audio.volume + 0.5;
      if (newVolumeLevel > 1) {
        newVolumeLevel = 0;
      }
      audio.volume = newVolumeLevel;
    });
  </script>
</html>
Comment 1 Radar WebKit Bug Importer 2022-09-08 07:37:15 PDT
<rdar://problem/99697508>
Comment 2 Eric Carlson 2022-09-16 09:40:21 PDT
A user gesture is required to change volume. Your code should should work if you move the part that changes volume inside of the `click` event handler
Comment 3 aerdogan07 2022-09-19 00:41:30 PDT
The volume is changed within the click event handler. It also works when there is no AudioContext. The same code also works with all other major browsers (Firefox, Chrome etc.) Therefore, I will reopen the ticket.