C#禁用Web浏览器声音/应用程序声音

时间:2015-01-29 14:42:09

标签: c# audio browser

我想禁用一个webbrowser声音,但我认为不可能,所以我看到有可能在比win xp更高的系统上禁用应用程序声音,现在我只需要知道怎么和我找不到它!

当前代码:

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

我不认为有一个webrowser.noSound的东西,我也使用activeform.hide()来隐藏webbrowser

1 个答案:

答案 0 :(得分:1)

首先添加此名称空间:

using System.Runtime.InteropServices;

现在您可以简单地禁用所有音频输出。试试这些代码:

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

更新:

你可以Read This其他人也回答了这个问题。

更新2:

  

您可以将其置于静态类中,并将CoInternetSetFeatureEnabled方法设为公共,或者在必要时从更可用的表单转换参数后添加另一个调用它的桥接方法。

阅读这两个相似的问题并禁用声音: Question1 Question2

更新3:

对于IE7及更高版本,您可以使用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);

以下是Source

更新4:

How to mute windows sound