question

PabloGlomby-5415 avatar image
0 Votes"
PabloGlomby-5415 asked JackJJun-MSFT answered

Error "The group or resource is not in the correct state to perform the requested operation" when doing WebView2.TrySuspendAsync

I basically need to pause a video when the WebView2 control loses its visibility.
What I did was this:

     /*********************************************************************/
     private async void SuspendWebView2(WebView2 webView2 )
     {
         bool bRes = await webView2.CoreWebView2.TrySuspendAsync();
     }


     /*********************************************************************/
     private void WebCtrl_VisibleChanged(object sender, EventArgs e)
     {
         WebView2 webView2 = (WebView2)sender;
         if (webView2.Visible == false && webView2.CoreWebView2.IsDocumentPlayingAudio )
         {
              webView2.CoreWebView2.IsMuted = true;
             SuspendWebView2(webView2);
         }
         else if (webView2.Visible == true && webView2.CoreWebView2.IsDocumentPlayingAudio )
         {
             webView2.CoreWebView2.IsMuted = false;
             webView2.CoreWebView2.Resume();
         }
     }

But I get:
System.InvalidOperationException: CoreWebView2 members cannot be accessed after the WebView2 control is disposed. ---> System.Runtime.InteropServices.COMException: The group or resource is not in the correct state to perform the requested operation. (Exception from HRESULT: 0x8007139F)

I tried a lot of things including hiding or showing the control (sometimes it works but it is not the same all the time), setting a timer and doing it few seconds after and so on....

Else, is there a way to pause a video? A generic solution like IsMuted=true for audio... Because I don't want to go inside the HTML since the video renderer may be different.

Thanks.

dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@PabloGlomby-5415, based on my test, I also reproduced your problem. Based on my research, it may be an issue for the webview2. I recommend that you could sumbit the issue in the Github.

A generic solution like IsMuted=true for audio... Because I don't want to go inside the HTML since the video renderer may be different.

Currently, I could not find other way to stop the video like the IsMuted property. I also find a workaround about the html method you mentioned.

You could refer to the following code to do it.

 private async void SuspendWebView2(WebView2 webView2)
         {
             string pausefunctionString = @"
     var videos = document.querySelectorAll('video');  
     [].forEach.call(videos, function(video) { video.pause(); });
     ";
             await webView2.ExecuteScriptAsync(pausefunctionString);
         }

However, as you said, the method may have certain flaws because their video renderer may be different.(based on my test, it works for YouTube video)

BTW, if you still want to use CoreWebView2.TrySuspendAsync method to solve it, I still recommend that you submit the issue in Github.



Best Regards,
Jack


If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.