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.