I want to be able to mute all other programs and play a constant tone/sound while a button is held down.
The tone/sound is attached as a recouce under My.Recources
and is called Beep_effect
.
Most of the code is from one of the answers but is still does not work.
This is the code I have so far:
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
If you have any other questions just ask!
Use CSCore and AudioClientShareMode.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