How to make a Volume Controller

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
7 posts Page 1 of 1
Contributors
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

How to make a Volume Controller
Usman55
Hello Everyone,

Today I'm going to show you how to make a simple volume controller which uses buttons to control the volume. It basically increases and decreases the volume by 5% or so and also has the feature to mute/unmute the volume. It is real simple coding but you should have some vb.net knowledge to understand it. I made this little application as a feature for my music application. So enjoy it.

---------------------------------------------------------------------------

First of all, open Visual Basic 2008, create a new project, choose Windows Forms Application, type in the textbox Volume Controller and press the OK button. As soon as you have done this, the Form1 will appear on the screen on which we will design our application and code it.


First we will do the designing and then the coding. So now we will change the Form1's properties to make it look good. Change the Form1's Text property to Volume Controller. Change it's StartPosition property to CenterScreen. Change it's FormBorderStyle property to FixedToolWindow. Change it's MaximizeBox and MinimizeBox property to False.


Now add the following things to the form:

(1)_ 3 Buttons. Change Button1's Text property to Increase, Button2's to Decrease and Button3's to Mute/Unmute.


Now is the time to code Form1 and it's components. So right-click on the form in the Solution Explorer and click View Code.

Type the following code before the Public Class Form1:
Code: Select all
Imports System.Runtime.InteropServices
Type the following code after the Public Class Form1:
Code: Select all
Private Const APPCOMMAND_VOLUME_MUTE As Integer = &H80000
    Private Const APPCOMMAND_VOLUME_UP As Integer = &HA0000
    Private Const APPCOMMAND_VOLUME_DOWN As Integer = &H90000
    Private Const WM_APPCOMMAND As Integer = &H319
<DllImport("user32.dll")> Public Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function
Add the following code to Button1 by double-clicking it:
Code: Select all
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_UP))
Add the following code to Button2 by double-clicking it:
Code: Select all
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_DOWN))
Add the following code to Button3 by double-clicking it:
Code: Select all
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_MUTE))

---------------------------------------------------------------------------

Great Job! You have just completed making a Volume Controller. Now debug and run it by pressing the Start Debugging button located at the main toolstrip. Test it, if it works fine then its SAVING time. You can save your project by going to File -> Save All and then pressing Save.

And there! You have your project and application that works. If you have any problem or can't understand something, please feel free to ask by either a comment or by PM. I have also attached a screenshot.

Please use the Give Thanks button and the Reputation System to appreciate my hard work. I have wrote this tutorial while making a File Downloader project myself so you can download the source file in the attachments.

Thank you.

Image
You do not have the required permissions to view the files attached to this post.
Image
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: How to make a Volume Controller
XTechVB
cool i really needed this for my new program. Thanks Dude, Nice Work
You can find me on Facebook or on Skype mihai_92b
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: How to make a Volume Controller
Usman55
Thanks for your compliments. Btw, what application are you making?
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: How to make a Volume Controller
mandai
This will work but using SendMessage is a lot like sending keys (the vol up/down buttons) to the application. You can use waveOutGetVolume and waveOutSetVolume to directly access the volume control.
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: How to make a Volume Controller
Usman55
I think I'm sticking with this but can you make me an example with a trackbar?
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: How to make a Volume Controller
mandai
Try:
Code: Select all
    <DllImport("winmm.dll")> Shared Function waveOutGetVolume(ByVal hwo As IntPtr, ByRef pdwVolume As UInteger) As UInteger
    End Function

    <DllImport("winmm.dll")> Shared Function waveOutSetVolume(ByVal hwo As IntPtr, ByVal pdwVolume As UInteger) As UInteger
    End Function

    Dim first As Boolean = True

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim vol As UInteger
        waveOutGetVolume(IntPtr.Zero, vol)
        Dim volBytes As Byte() = BitConverter.GetBytes(vol)

        Dim leftVol As UShort = BitConverter.ToUInt16(volBytes, 0) / (UShort.MaxValue / trackLeft.Maximum)
        trackLeft.Value = leftVol

        Dim rightVol As UShort = BitConverter.ToUInt16(volBytes, 2) / (UShort.MaxValue / trackRight.Maximum)
        trackRight.Value = rightVol
        first = False
    End Sub

    Private Sub track_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trackRight.Scroll, trackLeft.Scroll
        If Not first Then
            Dim leftVol As UShort = trackLeft.Value * (UShort.MaxValue / trackLeft.Maximum)
            Dim leftBytes As Byte() = BitConverter.GetBytes(leftVol)

            Dim rightVol As UShort = trackRight.Value * (UShort.MaxValue / trackRight.Maximum)
            Dim rightBytes As Byte() = BitConverter.GetBytes(rightVol)

            'Label1.Text = leftVol & " " & rightVol 'debug

            Dim volBytes As Byte() = {leftBytes(0), leftBytes(1), rightBytes(0), rightBytes(1)}
            Dim vol As UInteger = BitConverter.ToUInt32(volBytes, 0)
            waveOutSetVolume(IntPtr.Zero, vol)

        End If
    End Sub
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: How to make a Volume Controller
Usman55
Thats long code, I'll try after I come from school or maybe tomorrow.
Image
7 posts Page 1 of 1
Return to “Tutorials”