Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Get and set the wave sound volume

You will learn how to retrieve and change the current wave volume of the sound card by using waveOutGetVolume() and waveOutSetVolume() from the unmanaged Windows API in C# through P/Invoke.

On Tuesday, April 4th 2006 at 11:21 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.8 with 47 votes)
Contextual Ads
More C# Resources
Advertisement
You will learn how to retrieve and change the current wave volume of the sound card by using waveOutGetVolume() and waveOutSetVolume() from the unmanaged Windows API in C# through P/Invoke.

Download this Visual Studio 2005 project Download this project (Visual Studio 2005)

Volume Control

The .NET Framework has a multitude of methods you can use to retrieve and set different system settings, however controlling the output volume of the soundcard is not one of them. As a result, we will have no other option than to call the unmanaged Windows API, more exactly the waveOutGetVolume() and waveOutSetVolume() functions from the winmm.dll library.

Start by creating a Windows Application project inside Visual Studio 2005. You can use Visual Studio 2003 just as well, only that the project attached to this tutorial was created inside Visual Studio 2005.
Inside the Windows Application form, place a label and TrackBar object entitled trackWave which we'll use to change the sound volume.

TrackBar Object

The form with its label and trackWave TrackBar should look similar to the following:

Wave Volume Control Form

You can, by the way, change the orientation of the TrackBar object to vertical so that it looks like the one in the popular Volume Control Windows application, using the Orientation property. There's no need to change other properties of the TrackBar control - the Minimum and Maximum properties should be left to their default values, 0 and 10 respectively. This will give us 10 steps to control the volume of the wave sound. Obviously, you can later tweak this to your preference, in case you want to give the user the possibility of a finer volume level adjustment.

Switch to code view, and add the following using directive, since we'll be calling an unmanaged DLL:

using System.Runtime.InteropServices;


Now that we have this using statement in place, we can call the two functions from the winmm.dll. This DLL is located in the Windows System32 directory and has a size of about 172 KB.

[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);

[DllImport("winmm.dll")]
public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);


Now we are ready to make function calls, but unlike other Windows API calls, these two are a little more difficult and we need to do a few calculations before being able to get and set the volume.

To make our application work correctly, the first thing we need to do is retrieve the current sound volume level and set it on our TrackBar (trackWave), otherwise it will always stay at its default value of 0. We want to do that when the application loads, so that the user immediately sees a well adjusted TrackBar when the application has started.
Thus, inside the constructor of our form (Form1()) right after InitializeComponent() place the following lines of code:

// By the default set the volume to 0
uint
CurrVol = 0;
// At this point, CurrVol gets assigned the volume
waveOutGetVolume(IntPtr.Zero, out CurrVol);
// Calculate the volume
ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
// Get the volume on a scale of 1 to 10 (to fit the trackbar)
trackWave.Value = CalcVol / (ushort.MaxValue / 10);


At this time you can compile the application, and you should see the trackbar adjusted to your current wave volume level.
Now we are ready to change the volume using waveOutSetVolume(). We will do that in the Scroll event of trackWave (the TrackBar). To get to that event handler, you can simply double click the control in the form designer. Now inside the trackWave_Scroll() event handler, use the following lines of code:

// Calculate the volume that's being set
int NewVolume = ((ushort.MaxValue / 10) * trackWave.Value);
// Set the same volume for both the left and the right channels
uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
// Set the volume
waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);


Now you should be able to compile the application successfully and move the trackbar to adjust the wave volume.

Here is the complete code of Form1.cs, in case you want to have an overall look:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace VolumeControl
{
   public partial class Form1 : Form
   {

      [DllImport("winmm.dll")]
      public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);

      [DllImport("winmm.dll")]
      public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);


      public Form1()
      {
         InitializeComponent();
         // By the default set the volume to 0
         uint CurrVol = 0;
         // At this point, CurrVol gets assigned the volume
         waveOutGetVolume(IntPtr.Zero, out CurrVol);
         // Calculate the volume
         ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
         // Get the volume on a scale of 1 to 10 (to fit the trackbar)
         trackWave.Value = CalcVol / (ushort.MaxValue / 10);
      }

      private void trackWave_Scroll(object sender, EventArgs e)
      {
         // Calculate the volume that's being set
         int NewVolume = ((ushort.MaxValue / 10) * trackWave.Value);
         // Set the same volume for both the left and the right channels
         uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
         // Set the volume
         waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
      }

   }
}

Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by German Gachevski on Thursday, November 16th 2006 at 04:24 AM

Awesome tutorial! Now let\'s see if I can get it to work...

by Andrej Schäfer on Tuesday, December 18th 2007 at 04:54 PM

Nice tutorial! Very well done!
Gonna try it...

by Jared on Friday, January 25th 2008 at 09:00 PM

Thanks, I've been looking for some low level volume calls in C# to write in CLI for a couple of weeks. This is perfect.

by belal on Sunday, February 3rd 2008 at 05:03 PM

it\\\'s a good way
you are the most intelligance programmer

by talip on Thursday, February 14th 2008 at 08:23 AM

thanks. do you know how to reach the other controls such as front,rear controls?

by tlx on Monday, March 10th 2008 at 05:13 PM

Excellent!
Add-on: Just use CoreDll.dll to make the same code work on .net Compact Framework.

by jeremiah alfa on Wednesday, July 16th 2008 at 02:02 AM

thank 4 it!!!!!! success 4 us

by JeanValxa on Wednesday, August 13th 2008 at 03:06 AM

Thanks very much. It is easy to understand and work well. It is greate.

Next:- can you provide how can I concatnate to wave files? thanks

by Clau on Thursday, November 13th 2008 at 11:49 AM

Very good and straight tutorial. Now we need just how to have a more adjustment ;), then change the whay it looks like =) (little more programming).

by phil on Wednesday, December 3rd 2008 at 01:13 AM

not working in Vista 64bit...

by Khamza Davletov on Saturday, January 10th 2009 at 12:30 PM

It did not work for me. Even the compiled example.
Shit, why everybody just got euphoric for this piece of shit!?

by Khamza Davletov on Saturday, January 10th 2009 at 12:33 PM

... like "great, great. You're the one!! Let me kiss ur ass!". Is this the way, to thank a person in your country?

by Pheobie Danish on Sunday, February 1st 2009 at 09:15 PM

heeyy ya'll whats happenin ha ha ha ::::

by fael on Saturday, February 14th 2009 at 10:01 AM

uhmm... i got a question... where can i find everything that can be found in winmm.dll?

by Omid on Monday, March 16th 2009 at 04:46 PM

http://pinvoke.net/

by Ben on Monday, April 20th 2009 at 10:08 AM

Thanks very much, very well written!

by Searock on Tuesday, April 21st 2009 at 02:05 AM

thanks a lot:)

by Isaac on Saturday, May 30th 2009 at 12:05 PM

Thanks I finally made it work on my project.

by Isaac on Saturday, May 30th 2009 at 12:05 PM

Thanks I finally made it work on my project.

by Isaac on Saturday, May 30th 2009 at 12:05 PM

Thanks I finally made it work on my project.

by Isaac on Saturday, May 30th 2009 at 12:05 PM

Thanks I finally made it work on my project.

by Isaac on Saturday, May 30th 2009 at 12:05 PM

Thanks I finally made it work on my project.

by Isaac on Saturday, May 30th 2009 at 12:05 PM

Thanks I finally made it work on my project.

by Isaac on Saturday, May 30th 2009 at 12:05 PM

Thanks I finally made it work on my project.

by Isaac on Saturday, May 30th 2009 at 12:05 PM

Thanks I finally made it work on my project.

by Isaac on Saturday, May 30th 2009 at 12:05 PM

Thanks I finally made it work on my project.

by killcommander on Sunday, June 7th 2009 at 09:04 AM

nice tutorial, but for lazy peaple i made class for it:

using System;
using System.Runtime.InteropServices;

namespace WindowsTools
{
public class AudioMixerHelper
{

[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);

[DllImport("winmm.dll")]
public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);



public static int getVolume() {

// By the default set the volume to 0
uint CurrVol = 0;
// At this point, CurrVol gets assigned the volume
waveOutGetVolume(IntPtr.Zero, out CurrVol);
// Calculate the volume
ushort CalcVol = (ushort)(CurrVol

by killcommander on Sunday, June 7th 2009 at 09:06 AM

part 2:

by Dr Vague on Saturday, June 27th 2009 at 08:12 AM

Perfect solution, most others had too many features where I needed only one and this fit exactly, many thanks.

by Vekky on Saturday, July 4th 2009 at 08:14 AM

Awesome tutorial! But can you help me with one little thing... Could you help me how to put in this project checkbox Mute. Should I write code in CheckedChanged event? I could use some help with the code for mute itself! Thanks in advance!

by Mahmud on Monday, October 5th 2009 at 09:30 AM

Nice tutorial. But in my case it always gives the maximum volume irrespective of the sound card volume. I am running windows vista on a dell inspiron 15 laptop. Can't find out what's wrong. :S

Can anyone help?

by Kris on Wednesday, November 18th 2009 at 10:50 AM

What about master volume control?

by James on Monday, December 28th 2009 at 07:48 PM

I have almost finished a audio/video/picture player in C#, can anyone tell me how to simply code a trackbar to the timing of my media? I have a openFileDialog opening media to a textbox, then you press play and it plays. I need the trackbar to be syncronized with the media timing...Any thoughts or code???

by somnath chaturvedi on Monday, July 19th 2010 at 04:05 PM

this tutoril is very good.and articles is best

by Yanish on Saturday, July 31st 2010 at 01:11 AM

Works perfect.
@Khamza - maybe this is caused by your poor skills... who knows... Works for everyone but you...

@Everybody
If you making an app playing some multimedia (mp3, wav, etc) you can use old good MCI. It doesn't affect master volume, but is very usefull when making own player.

by Twaha on Tuesday, August 3rd 2010 at 03:15 AM

Hi, thanks a lot. It worked!

I actually modified this code a little bit to work as an app on windows mobile. It changes the phones volume, which is awesome. However, is there anyway this code can be changed to change the in-call volume too? There are 2 scroll bars, one of phone, the other of calls. The phone one changes when I change the slider, but I also want to change the call volume> Any help on that?

Thanks a lot once again!

by Twaha on Tuesday, August 3rd 2010 at 03:16 AM

Hi, thanks a lot. It worked!

I actually modified this code a little bit to work as an app on windows mobile. It changes the phones volume, which is awesome. However, is there anyway this code can be changed to change the in-call volume too? There are 2 scroll bars, one of phone, the other of calls. The phone one changes when I change the slider, but I also want to change the call volume> Any help on that?

Thanks a lot once again!

by Twaha on Tuesday, August 3rd 2010 at 03:16 AM

Hi, thanks a lot. It worked!

I actually modified this code a little bit to work as an app on windows mobile. It changes the phones volume, which is awesome. However, is there anyway this code can be changed to change the in-call volume too? There are 2 scroll bars, one of phone, the other of calls. The phone one changes when I change the slider, but I also want to change the call volume> Any help on that?

Thanks a lot once again!

by Twaha on Tuesday, August 3rd 2010 at 03:16 AM

Hi, thanks a lot. It worked!

I actually modified this code a little bit to work as an app on windows mobile. It changes the phones volume, which is awesome. However, is there anyway this code can be changed to change the in-call volume too? There are 2 scroll bars, one of phone, the other of calls. The phone one changes when I change the slider, but I also want to change the call volume> Any help on that?

Thanks a lot once again!

by Twaha on Tuesday, August 3rd 2010 at 03:16 AM

Hi, thanks a lot. It worked!

I actually modified this code a little bit to work as an app on windows mobile. It changes the phones volume, which is awesome. However, is there anyway this code can be changed to change the in-call volume too? There are 2 scroll bars, one of phone, the other of calls. The phone one changes when I change the slider, but I also want to change the call volume> Any help on that?

Thanks a lot once again!

by Twaha on Tuesday, August 3rd 2010 at 03:16 AM

Hi, thanks a lot. It worked!

I actually modified this code a little bit to work as an app on windows mobile. It changes the phones volume, which is awesome. However, is there anyway this code can be changed to change the in-call volume too? There are 2 scroll bars, one of phone, the other of calls. The phone one changes when I change the slider, but I also want to change the call volume> Any help on that?

Thanks a lot once again!

by wansook on Thursday, September 2nd 2010 at 11:18 PM

hi...
the code is work well for my project.. thanks alot
but a little problem here is that.. it effect my master volume control.. when i start running the application it always reset my WAV to nearly Zero...
anyway to solve it??

Thanks again for a nice tutorial!!


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources