Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webbrowser disable all audio output - from online radio to youtube

Tags:

browser

c#

wpf

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.

like image 520
Finchsize Avatar asked Mar 30 '13 16:03

Finchsize


1 Answers

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");
        }
    }
}
like image 102
noseratio Avatar answered Oct 31 '22 07:10

noseratio