【问题标题】:How to Mute WebView control (disable sounds)?如何静音 WebView 控件(禁用声音)?
【发布时间】:2017-10-02 01:17:45
【问题描述】:

我试图找到如何在我的 UWP 应用程序中使 WebView 控件静音。

该应用程序包含用于显示广告的 WebView 控件。当用户禁用应用程序声音时,播放任何声音都不好。但有些广告有声音,用户不满意。

我尝试将 ElementSoundPlayer.State 设置为 ElementSoundPlayerState.Off 但没有帮助。

提前谢谢你。

【问题讨论】:

  • 可以给个code吗?
  • 代码很简单。只需要静音 webview 声音 XAML:<WebView x:Name="_View" />CS:_View.Navigate(new Uri("https://www.youtube.com/watch?v=Z9orePtm6go"));

标签: c# webview uwp


【解决方案1】:

目前,没有这样的 api 用于静音 WebView Control 。对于您的场景,您可以通过使用 InvokeScriptAsync 注入可以静音的 JavaScript。请参考以下代码。

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 });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多