I am using a WinForms application and within that I created a WebView2 instance to play media content using HTML5. I have been looking into documentation to find some way by which I can Enable AutoPlay in WebView2 such that my desktop application will play videos without user's interaction.
Things that I have tried.
Thank you!
After a thorough research, I found a solution as well as my team lead also found a solution.
The first solution that I sorted, is a workaround to perform a click on WebView2 window right after you initialize the WebView2, using this SO Answer. This can be achieved by registering to WebView2.NavigationCompleted
event.
The second solution is more elegant and appropriate; although it needs final touches, anyone can improve it according to their own preferences.
var options = new CoreWebView2EnvironmentOptions("--autoplay-policy=no-user-gesture-required");
var path = @"C:\Program Files (x86)\Microsoft\EdgeWebView\Application\92.0.902.67";
var environment = await CoreWebView2Environment.CreateAsync(path, null, options);
var webView = new WebView2();
await webView.EnsureCoreWebView2Async(environment);
Note that the variable "path" contains the directory where your WebView2 runtime executive file is located. The path will be changed when a new version is released by Microsoft. You can also download a WebView2 fixed runtime and include those within your project dependencies.