问题 Web浏览器禁用所有音频输出 - 从在线广播到youtube


我的网络浏览器:

XAML:

//...
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
//...
<my:WindowsFormsHost Name="windowsFormsHost"/>

C#背后的代码:

System.Windows.Forms.WebBrowser Browser = new System.Windows.Forms.WebBrowser();
windowsFormsHost.Child = Browser;

我的问题是如何禁用所有音频输出。

我找到了这个:

C#:

private const int Feature = 21; //FEATURE_DISABLE_NAVIGATION_SOUNDS
private const int SetFeatureOnProcess = 0x00000002;

[DllImport("urlmon.dll")]
[PreserveSig]
[return: MarshalAs(UnmanagedType.Error)]
static extern int CoInternetSetFeatureEnabled(int featureEntry,
  [MarshalAs(UnmanagedType.U4)] int dwFlags, 
  bool fEnable);

它很好,但这段代码只能禁用“咔嗒”声,所以在这种情况下它的用处没用。

我只想从我的应用程序100%静音,根本没有声音。

我已经读过,在这个webbrowser中,它需要通过Windows Sounds完成,但我真的不知道我不能在代码中执行此操作。


11396
2018-03-30 16:27


起源



答案:


以下是如何轻松完成的工作。但不是特定于WebBrowser,而是按照您的要求执行: 我只想从我的应用程序100%静音,根本没有声音。

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WinformsWB
{
    public partial class Form1 : Form
    {
        [DllImport("winmm.dll")]
        public static extern int waveOutGetVolume(IntPtr h, out uint dwVolume);

        [DllImport("winmm.dll")]
        public static extern int waveOutSetVolume(IntPtr h, uint dwVolume);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // save the current volume
            uint _savedVolume;
            waveOutGetVolume(IntPtr.Zero, out _savedVolume);

            this.FormClosing += delegate 
            {
                // restore the volume upon exit
                waveOutSetVolume(IntPtr.Zero, _savedVolume);
            };

            // mute
            waveOutSetVolume(IntPtr.Zero, 0);
            this.webBrowser1.Navigate("http://youtube.com");
        }
    }
}

10
2017-08-21 02:42



不幸的是,我错过了OP问题中有关将整个应用程序静音的部分;我提供了一个赏金,希望有一个特定于Web浏览器控件的解决方案(因为我的应用程序有其他音频源)。您不会碰巧知道更有针对性的方法吗?无论哪种方式,这个 不 很好地回答OP问题。 - JYelton
我不认为这是可能的。我试过发送 APPCOMMAND_VOLUME_MUTE 到整个孩子的窗口树 WebBrowser,最好的结果是全球静音。 - Noseratio
我向你颁发了奖金,至少这是任何人都能提出的最佳解决方案。谢谢。 - JYelton


答案:


以下是如何轻松完成的工作。但不是特定于WebBrowser,而是按照您的要求执行: 我只想从我的应用程序100%静音,根本没有声音。

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WinformsWB
{
    public partial class Form1 : Form
    {
        [DllImport("winmm.dll")]
        public static extern int waveOutGetVolume(IntPtr h, out uint dwVolume);

        [DllImport("winmm.dll")]
        public static extern int waveOutSetVolume(IntPtr h, uint dwVolume);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // save the current volume
            uint _savedVolume;
            waveOutGetVolume(IntPtr.Zero, out _savedVolume);

            this.FormClosing += delegate 
            {
                // restore the volume upon exit
                waveOutSetVolume(IntPtr.Zero, _savedVolume);
            };

            // mute
            waveOutSetVolume(IntPtr.Zero, 0);
            this.webBrowser1.Navigate("http://youtube.com");
        }
    }
}

10
2017-08-21 02:42



不幸的是,我错过了OP问题中有关将整个应用程序静音的部分;我提供了一个赏金,希望有一个特定于Web浏览器控件的解决方案(因为我的应用程序有其他音频源)。您不会碰巧知道更有针对性的方法吗?无论哪种方式,这个 不 很好地回答OP问题。 - JYelton
我不认为这是可能的。我试过发送 APPCOMMAND_VOLUME_MUTE 到整个孩子的窗口树 WebBrowser,最好的结果是全球静音。 - Noseratio
我向你颁发了奖金,至少这是任何人都能提出的最佳解决方案。谢谢。 - JYelton


您可以尝试使用 DISPID_AMBIENT_DLCONTROL

DLCTL_DLIMAGES,DLCTL_VIDEOS和DLCTL_BGSOUNDS:如果设置了这些标志,将从服务器下载图像,视频和背景声音并显示或播放。如果未设置标志,则不会下载和显示它们。


0
2017-08-29 18:39