Reputation: 83
I try to find how to mute WebView control in my UWP application.
The App contains WebView control to show advertisements. When user disables app sounds it is not good to play any sounds. But some ads have sound and users are unhappy.
I've tried set ElementSoundPlayer.State as ElementSoundPlayerState.Off but it does not help.
Thank you in advance.
Upvotes: 3
Views: 3254
Reputation: 21
Now we can mute/unmute by calling their API directly via:
webView.CoreWebView2.IsMuted=true;
This will mute the browser webview.
Upvotes: 2
Reputation: 32775
Currently, there is no such api for muting WebView Control . For your scenario, you could inject JavaScript that could mute sounds by using InvokeScriptAsync
. Please could refer to the following code.
private async void PassDataBtn_Click(object sender, RoutedEventArgs e)
{
string mutefunctionString = @"
var videos = document.querySelectorAll('video'),
audios = document.querySelectorAll('audio');
[].forEach.call(videos, function(video) { video.muted = true; });
[].forEach.call(audios, function(audio) { audio.muted = true; }); ";
await MyWebView.InvokeScriptAsync("eval", new string[] { mutefunctionString });
}
Upvotes: 6