Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# audio mu
I have a small piece of code that I used to change the wave volume of on windows XP. I used
 
[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);
 
When I use this code on windwos 7. and change volume
 
int NewVolume = ((ushort.MaxValue / 10) * currentVolume);
// 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);
 
The volume is not getting changed. When i opened the master volume window of win7 I could see the volume level bar getting changed but the volume change was not happening.
 
I know there are other ways to do the same thing on windows 7 but I was just wondering why the volume is not changing if my function call is able to set the volume bar to the required level?
Posted 29-Feb-12 1:41am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
since Windows Vista/7 MS changed the access to control volume, mixers, etc. and it's called the coreaudio API.
Here are some links, I'm sure you can work something out with it:
 
http://msdn.microsoft.com/en-us/library/ms678715.aspx?ppud=4[^]
Vista Core Audio API Master Volume Control[^]
http://rongchaua.net/blog/c-adjust-master-volume-in-vista-and-windows-7/[^]
 
You can also use or have a look at NAudio Wrapper:
http://naudio.codeplex.com/[^]
 
Good Luck Wink | ;)
Regards
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I made the experience that the waveOutSetVolume and waveOutGetVolume Functions only change the volume ouput of your current application and not the master system volume. therefore the volume is raised or lowered but you dont see any change on the master volume scrollbar. open the mixer and take a look there.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Okay..I have got some code...Feel free to alter it...after all I am an eleven year old programmer, and my concept of 'Copyright' is a little shaky.
Importing:
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
The Code:
<DllImport("winmm.dll")> _
		Public Shared Function waveOutGetVolume(hwo As IntPtr, ByRef dwVolume As UInteger) As Integer
		End Function
 
		<DllImport("winmm.dll")> _
		Public Shared Function waveOutSetVolume(hwo As IntPtr, dwVolume As UInteger) As Integer
		End Function
' Calculate the volume that's being set
        Dim NewVolume As Integer = ((UShort.MaxValue / 10) * TrackBar1.Value)
        ' Set the same volume for both the left and the right channels
        Dim NewVolumeAllChannels As UInteger = ((CUInt(NewVolume) And &HFFFF) Or (CUInt(NewVolume) << 16))
        ' Set the volume
        waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels)
Right..I better go.
 

 
-TheVBMax
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.150302.1 | Last Updated 19 Feb 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100