按下按钮时将除我以外的所有其他程序静音

Mute all other programs except mine whilst button is pressed

我希望能够在按住按钮时将所有其他程序静音并播放恒定的 tone/sound。

tone/sound 附在 My.Recources 下,称为 Beep_effect

大部分代码来自其中一个答案,但仍然不起作用。

这是我目前的代码:

Imports CSCore
Imports CSCore.Codecs
Imports CSCore.CoreAudioAPI
Imports CSCore.SoundOut
Imports System.Runtime.InteropServices

Module Module1

    Private filename As Object = My.Resources.Beep_effect
    Private _waveSource As IWaveSource
    Private _soundOut As ISoundOut
    Private device As MMDevice

    Sub Main()

        Using mmdeviceEnumerator = New MMDeviceEnumerator()
            Using mmdeviceCollection = mmdeviceEnumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active)
                device = mmdeviceCollection.First
            End Using
        End Using

        _waveSource = CodecFactory.Instance.GetCodec(filename).ToSampleSource().ToMono().ToWaveSource()
        _soundOut = New WasapiOut(False, AudioClientShareMode.Exclusive, 100) With {.device = device}
        _soundOut.Initialize(_waveSource)
        _soundOut.Play()

    End Sub

End Module

Public Class Main



    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function

    Const WM_APPCOMMAND As UInteger = &H319
    Const APPCOMMAND_VOLUME_MUTE As UInteger = &H8

    Private Sub Beep_MouseDown(sender As Object, e As EventArgs) Handles Beep.MouseDown
        Call Module1.Main()
        'My.Computer.Audio.Play(My.Resources.Beep_effect, AudioPlayMode.BackgroundLoop)
    End Sub

    Private Sub Beep_MouseUp(sender As Object, e As EventArgs) Handles Beep.MouseUp
        'My.Computer.Audio.Stop()
    End Sub

    Private Sub Silence_MouseDown(sender As Object, e As EventArgs) Handles Silence.MouseDown
        SendMessage(Handle, WM_APPCOMMAND, &H200EB0, APPCOMMAND_VOLUME_MUTE * &H10000)
    End Sub

    Private Sub Silence_MouseUp(sender As Object, e As EventArgs) Handles Silence.MouseUp
    SendMessage(Handle, WM_APPCOMMAND, &H200EB0, APPCOMMAND_VOLUME_MUTE * &H10000)
    End Sub
End Class

如果您有任何其他问题,请尽管提问!

使用CSCoreAudioClientShareMode.Exclusive

Imports CSCore
Imports CSCore.Codecs
Imports CSCore.CoreAudioAPI
Imports CSCore.SoundOut

Module Module1

    Private filename As String = "your.mp3"
    Private _waveSource As IWaveSource
    Private _soundOut As ISoundOut
    Private device As MMDevice

    Sub Main()

        Using mmdeviceEnumerator = New MMDeviceEnumerator()
            Using mmdeviceCollection = mmdeviceEnumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active)
                device = mmdeviceCollection.First
            End Using
        End Using

        _waveSource = CodecFactory.Instance.GetCodec(filename).ToSampleSource().ToMono().ToWaveSource()
        _soundOut = New WasapiOut(False, AudioClientShareMode.Exclusive, 100) With {.Device = device}
        _soundOut.Initialize(_waveSource)
        _soundOut.Play()

    End Sub

End Module