Control Sound With Visual Basic


   Control           Property    Setting
   ----------------------------------------

   Command Button    Name        PlaySound
                     Caption     Play Sound

   Command Button    Name        LeftUp
                     Caption     Left Up

   Command Button    Name        LeftDown
                     Caption     Left Down

   Command Button    Name        RightUp
                     Caption     Right Up

   Command Button    Name        RightDown
                     Caption     Right Down

   Label             Name        LeftVol

   Label             Name        RightVol

‘General Declarations
Declare Function sndPlaySound Lib "MMSYSTEM.DLL"
      (ByVal lpszSoundName As String, ByVal wFlags As Integer) As Integer
   Declare Function waveoutSetVolume Lib "mmsystem.dll"
      (ByVal wDeviceID As Integer, ByVal dwVolumeRight As Integer,
      ByVal dwVolumeLeft As Integer) As Integer
   Declare Function waveOutGetVolume Lib "MMSYSTEM.DLL"
      (ByVal wDeviceID As Integer, lpdwvolume As Long) As Integer

   Const SND_ASYNC = &H1
   Const SND_NODEFAULT = &H2

   Dim CurrentVolLeft As Long
   Dim CurrentVolRight As Long
                               
‘code
Sub Form_Load ()
   Dim x As Integer
   Dim BothVolumes As Long

 ' Note that the waveid is 0 indicating the first wave output device.
 ' If you were to play multiple wavefiles on multiple wave output devices
 ' you would use 1 for the second wave output device, 2 for the third and
 ' so on.
 ' This code will retrieve the current volume setting

   x = waveOutGetVolume(0, BothVolumes)

   ' This code isolates the low-order word.
   ' Note that the value &HFFFF& is a Long Integer, which is the same
   ' as 0000FFFF, but because Visual Basic would automatically
   ' truncate this to FFFF, you must force the logical operation to use
   ' a four-byte Long Integer (0000FFFF) rather than a two-byte Integer
   ' (FFFF). This is accomplished by using the type casting
   ' character (&).
   CurrentVolLeft = BothVolumes And &HFFFF&

   ' This code isolates the high-order word.
   ' Enter the following two lines as one, single line:
   CurrentVolRight = ((BothVolumes And &HFFFF0000) / &H10000)
     And &HFFFF&

   LeftVol.Caption = Hex$(CurrentVolLeft)   ' Update the label with the
   RightVol.Caption = Hex$(CurrentVolRight) ' current volume settings.
End Sub

Sub PlaySound_Click ()
   Dim x As Integer
   Dim wFlags As Integer
   Dim SoundName As String
   SoundName = "C:\WINDOWS\MSREMIND.WAV"    ' Pick any wave file.
   wFlags = SND_ASYNC Or SND_NODEFAULT
   x = sndPlaySound(SoundName$, wFlags%)    ' Play the wave file.
End Sub

Sub LeftUp_Click ()
   ' Increase the left sound channel setting:
   Dim x As Integer
   CurrentVolLeft = CurrentVolLeft + &H1000&
   ' Prevent the channel setting from exceeding the maximum limit:
   If CurrentVolLeft > &HFFFF& Then CurrentVolLeft = &HFFFF&
   LeftVol.Caption = Format$(Hex$(CurrentVolLeft))
   ' Enter the following two lines as one, single line:
   x = waveoutSetVolume(0, CInt("&H" + Hex$(CurrentVolRight)),
      CInt("&H" + Hex$(CurrentVolLeft)))
End Sub

Sub LeftDown_Click ()
   ' Decrease the left sound channel setting:
   Dim x As Integer
   CurrentVolLeft = CurrentVolLeft - &H1000&
   ' Prevent the channel setting from dropping below the minimum limit:
   If CurrentVolLeft < &H0& Then CurrentVolLeft = &H0&
   LeftVol.Caption = Hex$(CurrentVolLeft)
   ' Enter the following two lines as one, single line:
   x = waveoutSetVolume(0, CInt("&H" + Hex$(CurrentVolRight)),
      CInt("&H" + Hex$(CurrentVolLeft)))
End Sub

Sub RightUp_Click ()
   ' Increase the right sound channel setting:
   Dim x As Integer
   CurrentVolRight = CurrentVolRight + &H1000&
   ' Prevent the channel setting from exceeding the maximum limit.
   If CurrentVolRight > &HFFFF& Then CurrentVolRight = &HFFFF&
   RightVol.Caption = Hex$(CurrentVolRight)
   ' Enter the following two lines as one, single line:
   x = waveoutSetVolume(0, CInt("&H" + Hex$(CurrentVolRight)),
      CInt("&H" + Hex$(CurrentVolLeft)))
End Sub

Sub RightDown_Click ()
   ' Decrease the right sound channel setting:
   Dim x As Integer
   CurrentVolRight = CurrentVolRight - &H1000&
   ' Prevent the channel setting from dropping below the minimum limit:
   If CurrentVolRight < 0 Then CurrentVolRight = 0
   RightVol.Caption = Hex$(CurrentVolRight)
   ' Enter the following two lines as one, single line:
   x = waveoutSetVolume(0, CInt("&H" + Hex$(CurrentVolRight)),
      CInt("&H" + Hex$(CurrentVolLeft)))
End Sub
 

2 responses to “Control Sound With Visual Basic

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s