2

i know this topic has been discussed here before but i still can't get it to work. i just need to get the current volume in my machine (i'll start there and i guess expanding the functionality of my project will be easier after) using the win 7 SDK and without using code from published projects like this. should i add a particular reference to my project? use dllimport? any help would be appreciated

Yoav
  • 3,326
  • 3
  • 32
  • 73

2 Answers2

4

Maybe with this : http://msdn.microsoft.com/en-us/library/ms679138%28VS.85%29.aspx (ISimpleAudioVolume Interface)

--EDIT --

This project seems to contain what you are searching for.

http://hintdesk.com/Web/Source/Adjust%20System%20Volume.zip

If not, you can go look here : Get Master Sound Volume in c#

or here : http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/05dc2d35-1d45-4837-8e16-562ee919da85

But the best code I found about this always return to the link you gave in your question. Good Luck!

--Edit2--

Or you can use FMOD (c++) with the help of this : Using FMOD for C#?
and this : http://sourceforge.net/projects/fmodnet/

Community
  • 1
  • 1
LolCat
  • 539
  • 1
  • 11
  • 24
  • thanks, but how do i reference to this interface from my c# code? i've read this article and found no clue about how to use it – Yoav Jun 05 '12 at 19:43
  • Or you can try this question : http://stackoverflow.com/questions/4629816/get-master-volume-in-windows-xp-vista-seven-the-one-increased-through-keyboard – LolCat Jun 05 '12 at 19:52
  • could you please explain how to use this in my c# code? 'HRESULT' is giving me an exception and i know that unmanaged code can be used in .net but is this the case? – Yoav Jun 05 '12 at 20:03
  • Sorry, After more researches, I found out this was c++ code ... I edited my post. – LolCat Jun 06 '12 at 12:53
0

Using the Core Audio APIs the following class will allow you to get and set the volume of the default audio endpoint for playback.

using CoreAudioApi;

public class SystemVolumeConfigurator
{
        private readonly MMDeviceEnumerator _deviceEnumerator = new MMDeviceEnumerator();
        private readonly MMDevice _playbackDevice;

        public SystemVolumeConfigurator()
        {
            _playbackDevice = _deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
        }

        public int GetVolume()
        {
            return (int)(_playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
        }

        public void SetVolume(int volumeLevel)
        {
            if (volumeLevel < 0 || volumeLevel > 100)
                throw new ArgumentException("Volume must be between 0 and 100!");

            _playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar = volumeLevel / 100.0f;
        }
}
egfconnor
  • 2,637
  • 1
  • 26
  • 44