C# Disable Webbrowser Sound/Application sound

2020-08-01 07:02发布

问题:

I would like to disable a webbrowser sound but i don't think it's possible so i saw that it was possible to disable an application sound on systems higher than win xp, now i just need to know how and i can't find it!

Current code :

Form.ActiveForm.Hide();
        webBrowser1.ScriptErrorsSuppressed = true;
        try
        {
            webBrowser1.Navigate(args[2], null, null, "User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Xbox; Xbox One)");
        }
        catch (Exception ex)
        {
            Environment.Exit(0);
        }

i don't think there is a webrowser.noSound thing , also i used activeform.hide() to hide the webbrowser

回答1:

First add this name space :

using System.Runtime.InteropServices;

Now you can simply disable all audio output. Try these codes :

[DllImport("winmm.dll")]
    public static extern int GetVolume(IntPtr p, out uint volume);

    [DllImport("winmm.dll")]
    public static extern int SetVolume(IntPtr p, uint volume);

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Save the current volume
        int save;
        GetVolume(IntPtr.Zero, out save);

        this.FormClosing += delegate 
        {
            // Restore the volume 
            SetVolume(IntPtr.Zero, save);
        };

        // Now you can mute sounds
        SetVolume(IntPtr.Zero, 0);
        string url = "http://www.example.com";
        this.webBrowser1.Navigate(url);
    }

Update :

You can Read This someone else also answered this question.

Update 2 :

You can put it inside a static class and either make the CoInternetSetFeatureEnabled method public, or add an additional bridge method that calls it after converting the parameters from a more usable form, if necessary.

Read these two similar questions and disable the sound : Question1 Question2

Update 3 :

For IE7 and above you can use CoInternetSetFeatureEnabled :

// Constants
private const int FEATURE_DISABLE_NAVIGATION_SOUNDS = 21;
private const int SET_FEATURE_ON_THREAD = 0x00000001;
private const int SET_FEATURE_ON_PROCESS = 0x00000002;
private const int SET_FEATURE_IN_REGISTRY = 0x00000004;
private const int SET_FEATURE_ON_THREAD_LOCALMACHINE = 0x00000008;
private const int SET_FEATURE_ON_THREAD_INTRANET = 0x00000010;
private const int SET_FEATURE_ON_THREAD_TRUSTED = 0x00000020;
private const int SET_FEATURE_ON_THREAD_INTERNET = 0x00000040;
private const int SET_FEATURE_ON_THREAD_RESTRICTED = 0x00000080;

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

......

// You can call the CoInternetSetFeatureEnabled like this:
CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_PROCESS, true);

Here is the Source

Update 4 :

How to mute windows sound



标签: c# audio browser