How to access audio as an AudioSource, adjust volume, or mute audio?

Accessing audio as an AudioSource (Windows & macOS)

Starting in v4.15, 3D WebView for Windows and macOS supports optionally outputting audio as a Unity AudioSource. You can enable that by clicking the "Output Audio as Audio Source" checkbox on your WebViewPrefab or CanvasWebViewPrefab in the editor, or by setting its AudioSourceEnabled field programmatically at runtime. Once enabled, scripts can control audio parameters on the AudioSource by accessing it via the WebViewPrefab.AudioSource property, like this:

webViewPrefab.AudioSourceEnabled = true;
await webViewPrefab.WaitUntilInitialized();
// Set the volume to half.
webViewPrefab.AudioSource.volume = 0.5f;
// Enable 3D spatial audio for the webview.
// The default is 2D audio (spatialBlend = 0f).
webViewPrefab.AudioSource.spatialBlend = 1f;

Alternatively, scripts can process audio data in a custom way using the IWithAudioStream interface.

Other platforms (e.g. Android, iOS)

3D WebView is only able to output audio as a Unity AudioSource on Windows and macOS. On other platforms (like Android and iOS), the browser engines can only output audio directly to the system and don't provide a way to access audio data. However, your application can still manipulate a web page's audio using JavaScript. For example, the following code uses IWebView.ExecuteJavaScript() to set the volume of all media elements on the page to 25%:

webViewPrefab.WebView.ExecuteJavaScript(
  "document.querySelectorAll('video, audio').forEach(mediaElement => mediaElement.volume = 0.25)"
);

For more information about how to manipulate media elements with JavaScript, please see MDN's HTMLMediaElement documentation.

Muting audio

Some of the 3D WebView packages support the IWithMutableAudio interface, which scripts can use to mute browser audio.