11

My webbrowser:

XAML:

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

Code behind C#:

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

My question is how to disable all audio output.

I found this:

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

Its fine, but this code disable only "click" sound, so its kind of useless in this case.

I just want from my application 100% mute, no sounds at all.

I've read that in this webbrowser it need to be done through Windows Sounds, but I cant really bielieve that I cant do this in code.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Finchsize
  • 935
  • 2
  • 17
  • 34

2 Answers2

11

Here is how you can do it with ease. Not specific to WebBrowser though, but does what you requested: I just want from my application 100% mute, no sounds at all.

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");
        }
    }
}
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • 2
    Unfortunately I missed the part in the OP question about having the entire application be mute; I offered a bounty hoping for a solution that was specific to the web browser control (since my app has other sources for audio). You wouldn't happen to know a more targeted method would you? Either way, this *does* answer the OP question quite well. – JYelton Aug 22 '13 at 16:52
  • I don't think it's possible. I've tried sending [APPCOMMAND_VOLUME_MUTE](http://stackoverflow.com/questions/154089/mute-windows-volume-using-c-sharp) to the whole child window tree of `WebBrowser`, and the best result was a global mute. – noseratio Aug 25 '13 at 06:47
  • 2
    I awarded the bounty to you, at least this is the best solution anyone's come up with. Thanks. – JYelton Aug 27 '13 at 16:21
0

You can try as well to use DISPID_AMBIENT_DLCONTROL

DLCTL_DLIMAGES, DLCTL_VIDEOS, and DLCTL_BGSOUNDS: Images, videos, and background sounds will be downloaded from the server and displayed or played if these flags are set. They will not be downloaded and displayed if the flags are not set.

volody
  • 6,946
  • 3
  • 42
  • 54