Change program volume on Win 7

I want to change the size of the program (not the master ). I have the following code right now:

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); private void volumeBar_Scroll(object sender, EventArgs e) { // Calculate the volume that being set int NewVolume = ((ushort.MaxValue / 10) * volumeBar.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); } 

This only works on Win XP, not on Windows 7 (and probably Vista). I did not find a single script that will achieve the same in Win 7, only to change the master volume (which I did not after).

+7
source share
2 answers

Your code worked fine for me (with a few settings). Here is the code for a very simple WPF test application running on Windows 7 x64:

Xaml

 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Slider Minimum="0" Maximum="10" ValueChanged="ValueChanged"/> </Grid> </Window> 

FROM#

 public partial class MainWindow { public MainWindow() { InitializeComponent(); } private void ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { // Calculate the volume that being set double newVolume = ushort.MaxValue * e.NewValue / 10.0; uint v = ((uint) newVolume) & 0xffff; uint vAll = v | (v << 16); // Set the volume int retVal = NativeMethods.WaveOutSetVolume(IntPtr.Zero, vAll); Debug.WriteLine(retVal); bool playRetVal = NativeMethods.PlaySound("tada.wav", IntPtr.Zero, 0x2001); Debug.WriteLine(playRetVal); } } static class NativeMethods { [DllImport("winmm.dll", EntryPoint = "waveOutSetVolume")] public static extern int WaveOutSetVolume(IntPtr hwo, uint dwVolume); [DllImport("winmm.dll", SetLastError = true)] public static extern bool PlaySound(string pszSound, IntPtr hmod, uint fdwSound); } 

When I launch the application and move the slider, an additional volume control appears in the "Volume Mixer", which synchronously moves from the slider to max. using the slider.

You should examine the return value from waveOutSetVolume. This may give you a clue if your code is still not working.

+6
source

You can use the IAudioVolume and IAudioSessionNotification audio file APIs to change the current application volume and to track your volume using the volume slider in the application.

You can find a list of patterns of their use in the blog article of Larry Osterman

The easiest to use ISimpleVolume interface. It was discussed on Larry's blog .

+1
source

All Articles