Changing master volume level

后端 未结 3 840
说谎
说谎 2020-11-27 04:55

How can I change the master volume level? Using this code

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


        
3条回答
  •  渐次进展
    2020-11-27 05:32

    Use this free library, it's simple and does the job. InputSimulator

    It simulates a key press. You only have to add this reference and call wherever you want static methods like these:

      InputSimulator.SimulateKeyPress(VirtualKeyCode.VOLUME_UP);
    
      InputSimulator.SimulateKeyPress(VirtualKeyCode.VOLUME_DOWN);
    
      InputSimulator.SimulateKeyPress(VirtualKeyCode.VOLUME_MUTE);
    

    Then, if you want to get the master vokume level do:

      // volume update
      MMDevice defaultDevice = new MMDeviceEnumerator()
            .GetDefaultAudioEndpoint(DataFlow.Render‌​, Role.Multimedia);
      // veloce attesa per l'aggiornamento del volume
      Thread.Sleep(100);
      float level = defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar; 
    

    Doing this you'll have in level the current volume in 0-1 format (eg. 52% is 0.52) If you want to have it in 0-100 format simply do level*100

提交回复
热议问题