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

音调/声音作为 list 附加在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

08-28 02:17