【问题标题】:Mute/Unmute Application Sounds静音/取消静音应用程序声音
【发布时间】:2013-09-15 17:58:01
【问题描述】:

当我使用的代码是:

My.Computer.Audio.Play(My.Resources.audio_here, AudioPlayMode.Background)

【问题讨论】:

    标签: .net winforms visual-studio-2010 visual-studio-2012


    【解决方案1】:
    [DllImport("winmm.dll")]
    private static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
    
    //mute application
    private void mute(){
    {
        int NewVolume = 0; //set 0 to unmute
        uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
        waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
    }
    
    //unmute application
    private void unmute(){
    {
        int NewVolume = 65535; //set 65535 to unmute
        uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
        waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
    }
    

    【讨论】:

      【解决方案2】:

      最后我花了 10 多个小时尝试不同的选项并将 C++ 和 C# 代码翻译成 VB.net,最后这似乎至少对我有用。自己试试吧

      Imports System.Runtime.InteropServices
      Imports Microsoft.Win32
      
      Public Class Form1
      
      <DllImport("winmm.dll")>
      Public Shared Function waveOutSetVolume(ByVal h As IntPtr, ByVal dwVolume As UInteger) As Integer
      End Function
      
      
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      
          Me.Show()
      
          Dim keyValue As String  'disable web click sound
          keyValue = "%SystemRoot%\Media\"
          If Environment.OSVersion.Version.Major = 5 AndAlso Environment.OSVersion.Version.Minor > 0 Then
              keyValue += "Windows XP Start.wav"
          ElseIf Environment.OSVersion.Version.Major = 6 Then
              keyValue += "Windows Navigation Start.wav"
          Else
              Return
          End If
          Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("AppEvents\Schemes\Apps\Explorer\Navigating\.Current", True)
          key.SetValue(Nothing, "", RegistryValueKind.ExpandString)
      
          Me.WebBrowser1.Navigate("https://www.youtube.com/some_video")
          waveOutSetVolume(IntPtr.Zero, 0)
      
      
      End Sub
      

      结束类

      【讨论】:

        【解决方案3】:

        看看winmm.dll中的waveOutSetVolumewaveOutGetVolume函数

        Private Declare Function waveOutSetVolume Lib "winmm.dll" (ByVal uDeviceID As Integer, ByVal dwVolume As Integer) As Integer
        Private Declare Function waveOutGetVolume Lib "winmm.dll" (ByVal uDeviceID As Integer, ByRef lpdwVolume As Integer) As Integer
        

        waveOutSetVolume Function

        waveOutGetVolume Function

        根据目标机器的操作系统版本,您可能需要考虑使用较新的 MMDeviceEndpointVolume API。

        EndpointVolume Sample Code

        【讨论】:

        • 如何获取 uDeviceID?
        • 我们总是能够为 uDeviceID 指定 0。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-23
        相关资源
        最近更新 更多