Используя данный код:
[DllImport("winmm.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
public static extern uint waveOutGetVolume(IntPtr hwo, out uint dwVolume);
[DllImport("winmm.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
public static extern uint waveOutSetVolume(int uDeviceID, uint dwVolume);
[DllImport("winmm.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
public static extern uint waveOutSetVolume(IntPtr uDeviceID, uint dwVolume);
[DllImport("winmm.dll", EntryPoint = "waveOutSetVolume")]
public static extern int WaveOutSetVolume(IntPtr hwo, uint dwVolume);
[DllImport("winmm.dll")]
public static extern int waveOutSetVolume(int wDeviceID, int dwVolume);
[DllImport("winmm.dll", SetLastError = true)]
public static extern bool PlaySound(string pszSound, IntPtr hmod, uint fdwSound);
public static void SetSoundVolume(int volume)
{
try
{
//int currVol = (int)(50 * 0xFFFF / (double)100);
//currVol += currVol << 16;
//waveOutSetVolume(0, currVol);
int newVolume = ushort.MaxValue / 10 * volume;
uint newVolumeAllChannels = ((uint)newVolume & 0x0000ffff) | ((uint)newVolume << 16);
//waveOutGetVolume(IntPtr.Zero, out newVolumeAllChannels);
waveOutSetVolume(IntPtr.Zero, newVolumeAllChannels);
}
catch { }
}
public static int GetSoundVolume()
{
int value = 0;
try
{
waveOutGetVolume(IntPtr.Zero, out uint curVolume);
ushort CalcVol = (ushort)(curVolume & 0x0000ffff);
value = CalcVol / (ushort.MaxValue /10);
}
catch { }
return value;
}
Как мне в моём приложение сделать громкость на половину?
Хотелось бы чтобы можно было изменять от 0 до 100 - это максимум.
AudioManager.SetApplicationVolume
кажется работает как-то не так или вообще не работает. Попробовал изменить так:AudioManager.SetApplicationVolume(Process.GetCurrentProcess().Id, 10f);
но громкость так и осталась в приложение.AudioManager
и управляет. Если в списке регуляторов вашего приложения нет, то и управлять соответственно нечем.float? volume = AudioManager.GetApplicationVolume(pid);
, если вернулоnull
, значит нет регулятора громкости для этогоpid
.