A 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.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This 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.
Creating 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.
Get and set the wave sound volumeYou 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) |
|||||
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. 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. ![]() The form with its label and trackWave TrackBar should look similar to the following: ![]() 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:
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.
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:
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:
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:
|
||||||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
||||||
|
||||||
Current CommentsAwesome tutorial! Now let\'s see if I can get it to work...
Nice tutorial! Very well done!
Gonna try it...
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.
it\\\'s a good way
you are the most intelligance programmer
thanks. do you know how to reach the other controls such as front,rear controls?
Excellent!
Add-on: Just use CoreDll.dll to make the same code work on .net Compact Framework.
thank 4 it!!!!!! success 4 us
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
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).
not working in Vista 64bit...
It did not work for me. Even the compiled example.
Shit, why everybody just got euphoric for this piece of shit!?
... like "great, great. You're the one!! Let me kiss ur ass!". Is this the way, to thank a person in your country?
heeyy ya'll whats happenin ha ha ha ::::
uhmm... i got a question... where can i find everything that can be found in winmm.dll?
http://pinvoke.net/
Thanks very much, very well written!
thanks a lot:)
Thanks I finally made it work on my project.
Thanks I finally made it work on my project.
Thanks I finally made it work on my project.
Thanks I finally made it work on my project.
Thanks I finally made it work on my project.
Thanks I finally made it work on my project.
Thanks I finally made it work on my project.
Thanks I finally made it work on my project.
Thanks I finally made it work on my project.
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
part 2:
Perfect solution, most others had too many features where I needed only one and this fit exactly, many thanks.
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!
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?
What about master volume control?
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???
this tutoril is very good.and articles is best
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.
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!
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!
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!
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!
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!
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!
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!!
Related Tutorials
Related Source Code
C# Job Search