3D WebView doesn't have C# APIs specifically for controlling video or audio within a web page, but you can control a web page's <video> and <audio> elements via JavaScript using IWebView.ExecuteJavaScript() or PageLoadScripts. For example, the following code runs JavaScript that pauses the first video on the web page:
// Get the first <video> element on the web page and then call its pause method.
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/pause
webViewPrefab.WebView.ExecuteJavaScript(@"
// Enclose in a block to prevent videoElement from being a global variable.
{
let videoElement = document.getElementsByTagName('video')[0];
videoElement.pause();
}
");
For a list of JavaScript APIs available for video and audio elements, please see the documentation for HTMLMediaElement on MDN. You can also use 3D WebView's message passing APIs to send media events to your C# scripts. Here's an example that notifies C# when an audio element finishes playing:
// Wait for the prefab to initialize before accessing its WebView property.
// https://developer.vuplex.com/webview/WebViewPrefab#WaitUntilInitialized
await webViewPrefab.WaitUntilInitialized();
// Add a script that gets the first <audio> element on the web page
// and attaches a listener to its "ended" event.
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/ended
webViewPrefab.WebView.PageLoadScripts.Add(@"
let audioElement = document.getElementsByTagName('audio')[0];
audioElement.addEventListener('ended', event => {
// Send a message to C# to notify it that audio has ended.
// https://support.vuplex.com/articles/how-to-send-messages-from-javascript-to-c-sharp
window.vuplex.postMessage('audio ended');
});
");
// Listen for the message from JavaScript.
// https://developer.vuplex.com/webview/IWebView#MessageEmitted
webViewPrefab.WebView.MessageEmitted += (sender, eventArgs) => {
if (eventArgs.Value == "audio ended") {
Debug.Log("The audio finished");
}
};
Some tips:
The examples above simply use document.getElementsbyTagName() to get the first <video> or <audio> element on the web page, but it's likely that you'll need more sophisticated JavaScript for your use case to get a reference to the correct element. I recommend using remote debugging to use the webview's developer tools to develop and debug your JavaScript.
Web browsers block audio from automatically playing without user interaction, so if you try to play a video or audio automatically with JavaScript, the browser will likely block it by default. However, you can enable autoplaying with Web.SetAutoplayEnabled().