3

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.

  • I changed the Registry following this Documentation without success
  • I also enabled the AutoPlay from within Microsoft Edge but it did not have any effect.

Thank you!

Imran Faruqi
  • 663
  • 9
  • 19
  • WebView2 is not Edge, it's based on Chromium. The autoplay Policy here is a bit stricter. You can play video in a ` – Jimi Aug 09 '21 at 10:01
  • I mean, e.g., ` – Jimi Aug 09 '21 at 10:23
  • It does auto-play with audio if you set the `Source` property to `new Uri([video file path])`. – Jimi Aug 09 '21 at 12:24
  • @Jimi Do you mean because of those square brackets which make it run? I don't think so. – Imran Faruqi Aug 10 '21 at 13:10
  • What square brackets? Do you mean this: `[video file path]`? That's the standard way to imply *"a value to be provided"*. Which of course translates to e.g, `webView2.Source = new Uri(@"C:\Temp\somefile.mp4");` – Jimi Aug 10 '21 at 14:43
  • New chromium browsers simply do not allow autoplay even if you pass autoplay flag. They allow autoplay with muted but I do not want that. I wanted a video to be auto played without audio muted. – Imran Faruqi Aug 10 '21 at 16:31
  • Yes, that's why I suggested to pass the Video `file:///` path directly to the `Source` property. In this case, the Video is auto-played with audio. The `muted` limitation is related to the HTML. About the *limitation* of Chromium browsers, I described this in my first comment. – Jimi Aug 10 '21 at 16:33
  • I see, thanks for the input. Unfortunately the file is coming from an API so the path will contain http. – Imran Faruqi Aug 11 '21 at 11:11
  • It doesn't matter, it's the same thing. You can pass the HTTP/HTTPS address to the Source property and it will auto-play with audio. Didn't you try it? – Jimi Aug 11 '21 at 11:17
  • I haven't actually. Sorry I didn't get a chance to do it. But I will. However, I believe this seems to be fixed in future as bypassing the browser's policy will not be possible always. – Imran Faruqi Aug 12 '21 at 04:26
  • @Jimi your tip did not work for me when I tested. – Imran Faruqi Aug 17 '21 at 14:51

2 Answers2

3

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.

Imran Faruqi
  • 663
  • 9
  • 19
  • I get a compiler error at: `var environment = await CoreWebView2Environment.CreateAsync(path, null, options);` Error message: "The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'." But why? The method *IS* awaitable. – Cesar Coll Sep 03 '21 at 06:37
  • In addition to have a Method with Task return type, you also have to put 'async' keyword there. Like: public async Task MyMethod() { ... your method body ... } – Imran Faruqi Sep 05 '21 at 14:14
  • 1
    It also works if you pass null as the first (path) argument to CreateAsync, which has the advantage that it's not tied to a particular version or install location of Edge. – JohnT Aug 24 '22 at 17:53
2

It can be so. It helped me

public MainWindow()
    {
        Environment.SetEnvironmentVariable("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS", "--autoplay-policy=no-user-gesture-required");
        InitializeComponent();
    }
FAR747
  • 121
  • 2