Author Topic: turn system sound volume up  (Read 4403 times)

0 Members and 1 Guest are viewing this topic.

BillZndl

  • Guest
turn system sound volume up
« on: August 12, 2015, 11:55:00 AM »
Autocad 2012, VS2010 net 3.5,C# Windows 7

Anyone know how to access/change the computer speaker volume from C# directly?
I've searched extensively and everything I find creates a form with sliders.
I'd like to turn the volume to full before I play an .mp3 file from a .dll netloaded into AutoCAD.
This code works for the instantiated media player but doesn't affect the speaker sound of the computer.
Trying to figure out if there's a way to get the handle for the main volume.

Code - C#: [Select]
  1. namespace Whistles
  2. {  
  3.  
  4.     public class Whistle
  5.     {    
  6.         public Whistle()
  7.         {          
  8.         }      
  9.  
  10.         [CommandMethod("Whistles", CommandFlags.Session)]
  11.  
  12.         public void PlayFile()
  13.         {
  14.            
  15.             System.Audio.Volume = 100; // for 100%          
  16.  
  17.             MediaPlayer mp = new MediaPlayer();
  18.             mp.Open(new Uri(@"G:/AutoCAD2012Support/VisualStudio2010/WhistleSounds/Samsung_whistle_ringtone.mp3", UriKind.Absolute));
  19.             mp.Play();
  20.  
  21.             System.Audio.Volume = 14;
  22.         }
  23.     }
  24. }
  25.  
  26. namespace System
  27. {
  28.     public partial class Audio
  29.     {
  30.         [DllImport("winmm.dll")]
  31.         static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);
  32.  
  33.         [DllImport("winmm.dll")]
  34.         static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
  35.  
  36.         public static int Volume
  37.         {
  38.             get
  39.             {
  40.                 uint CurrVol = 100;
  41.                 ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
  42.                 return CalcVol;/// (ushort.MaxValue / 100);
  43.             }
  44.             set
  45.             {                
  46.                 int NewVolume = value;
  47.                 uint NewVolumeAllChannels = ((uint)NewVolume & 0x0000ffff);
  48.                 waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
  49.             }
  50.         }
  51.     }  
  52. }                              
  53.  

BlackBox

  • King Gator
  • Posts: 3770
Re: turn system sound volume up
« Reply #1 on: August 12, 2015, 12:20:48 PM »
If rolling your own implementation instead of using something like NirCmd, P/Invoke User32, and use either SendMessageW() + VOLUME_UP const, or KeyBd_Event() + Keys.VolumeUp enum.
"How we think determines what we do, and what we do determines what we get."

BillZndl

  • Guest
Re: turn system sound volume up
« Reply #2 on: August 12, 2015, 04:58:14 PM »
Yes, I seem to be having some progress.
I think I am getting the correct window handle of the main window.
I am not sure if I am getting the correct msg.
still researching.  :|

Code - C#: [Select]
  1. public class Whistle
  2.     {    
  3.         public Whistle()
  4.         {          
  5.         }      
  6.  
  7.         [CommandMethod("Whistles", CommandFlags.Session)]
  8.  
  9.         public void PlayFile()
  10.         {          
  11.  
  12.             IntPtr hWnd = Process.GetProcessesByName("explorer")[0].MainWindowHandle;
  13.            
  14.             uint msg = 0x319;//<<< not sure about this one.
  15.  
  16.             UIntPtr VOLUME_UP = (UIntPtr)0x319; //0xA0000;
  17.             IntPtr lParam = IntPtr.Zero;
  18.  
  19.             IntPtr e = DirectVolume.VolUp.SendMessageW(hWnd, msg, VOLUME_UP, lParam);
  20.  
  21.             System.Windows.MessageBox.Show( "Value of e : " + e.ToString());
  22.  
  23.             if (e.ToInt32() > 0)
  24.             {
  25.                 MediaPlayer mp = new MediaPlayer();
  26.                 mp.Open(new Uri(@"G:/AutoCAD2012Support/VisualStudio2010/WhistleSounds/Samsung_whistle_ringtone.mp3", UriKind.RelativeOrAbsolute));
  27.                 mp.Play();
  28.             }
  29.         }
  30.     }
  31. }
  32. namespace DirectVolume
  33. {
  34.     public partial class VolUp
  35.     {      
  36.  
  37.         [DllImport("User32.dll")]
  38.         public static extern IntPtr FindWindow(String sClassName, String sAppName);        
  39.         [DllImport("User32.dll")]
  40.         public static extern IntPtr SendMessageW(IntPtr hWnd, uint msg, UIntPtr VOLUME_UP, IntPtr lParam);
  41.  
  42.  
  43.     //P/Invoke User32, and use either SendMessageW() + VOLUME_UP const, or KeyBd_Event() + Keys.VolumeUp enum.
  44.     //const int VOLUME_UP = 0xA0000;
  45.     }
  46. }
  47.  
  48.  

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: turn system sound volume up
« Reply #3 on: August 12, 2015, 09:46:43 PM »
Haha...this could be pretty messed up to do to fellow workers

BlackBox

  • King Gator
  • Posts: 3770
Re: turn system sound volume up
« Reply #4 on: August 13, 2015, 01:34:47 AM »
"How we think determines what we do, and what we do determines what we get."

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: turn system sound volume up
« Reply #5 on: August 13, 2015, 02:22:36 AM »
haha...that's great stuff

BillZndl

  • Guest
Re: turn system sound volume up
« Reply #6 on: August 13, 2015, 06:30:59 AM »
Haha...this could be pretty messed up to do to fellow workers

My primary goal wasn't to "mess" with co-workers but......
There is a guy that uses a ring tone that we all find annoying.
So I thought if I could make a few machines around him occasionally play the same ringtone,
it may help him look for another tone to use.
 
Trouble is, the .mp3's are barely audiable when a machine is on 15%-20%,
which most of the machines are becaus the beeps are much louder at that setting,
so I need to turn up the volume and down again after playing the mp3.




BlackBox

  • King Gator
  • Posts: 3770
Re: turn system sound volume up
« Reply #7 on: August 13, 2015, 08:33:56 AM »
Perhaps a simpler way of going about this, depending on your domain credentials, is to simply use PowerShell... See this thread for the Set-Speaker calls, among other options:

http://stackoverflow.com/questions/21355891/change-audio-level-from-powershell



... One member even wrote his own PowerShell CmdLet, which exposes:

  • Get-DefaultAudioDevice
  • Get-AudioDeviceList
  • Set-DefaultAudioDevice [-Index] <Int>
  • Set-DefaultAudioDevice [-Name] <String>
  • Set-DefaultAudioDevice [-InputObject] <AudioDevice>
  • Set-DefaultAudioDeviceVolume -Volume <float>
  • Get-DefaultAudioDeviceVolume
  • Set-DefaultAudioDeviceMute
  • Write-DefaultAudioDeviceValue [-StreamValue]

http://www.automatedops.com/projects/windowsaudiodevice-powershell-cmdlet/
"How we think determines what we do, and what we do determines what we get."

BillZndl

  • Guest
Re: turn system sound volume up
« Reply #8 on: August 13, 2015, 09:50:43 AM »
Yeah, well....
This might be a bit over my head and permissions, don't know.
I'll give it a try if it slows up around here.

Thanks!

Bill


Perhaps a simpler way of going about this, depending on your domain credentials, is to simply use PowerShell... See this thread for the Set-Speaker calls, among other options:

http://stackoverflow.com/questions/21355891/change-audio-level-from-powershell



... One member even wrote his own PowerShell CmdLet, which exposes:

  • Get-DefaultAudioDevice
  • Get-AudioDeviceList
  • Set-DefaultAudioDevice [-Index] <Int>
  • Set-DefaultAudioDevice [-Name] <String>
  • Set-DefaultAudioDevice [-InputObject] <AudioDevice>
  • Set-DefaultAudioDeviceVolume -Volume <float>
  • Get-DefaultAudioDeviceVolume
  • Set-DefaultAudioDeviceMute
  • Write-DefaultAudioDeviceValue [-StreamValue]

http://www.automatedops.com/projects/windowsaudiodevice-powershell-cmdlet/

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: turn system sound volume up
« Reply #9 on: August 13, 2015, 10:19:36 AM »
Haha...this could be pretty messed up to do to fellow workers

My primary goal wasn't to "mess" with co-workers but......
There is a guy that uses a ring tone that we all find annoying.
So I thought if I could make a few machines around him occasionally play the same ringtone,
it may help him look for another tone to use.
 
Trouble is, the .mp3's are barely audiable when a machine is on 15%-20%,
which most of the machines are becaus the beeps are much louder at that setting,
so I need to turn up the volume and down again after playing the mp3.

Until somebody has their headphones on and you give them temporary hearing loss.  Stuff like this rarely ends well.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

BlackBox

  • King Gator
  • Posts: 3770
Re: turn system sound volume up
« Reply #10 on: August 13, 2015, 12:15:23 PM »
... Stuff like this rarely ends well.

This.
"How we think determines what we do, and what we do determines what we get."

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: turn system sound volume up
« Reply #11 on: August 13, 2015, 01:57:53 PM »
... Stuff like this rarely ends well.

This.

Yep, as in most games a poke in the eye with a stick spoils everyone's fun  :|
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

BillZndl

  • Guest
Re: turn system sound volume up
« Reply #12 on: August 14, 2015, 07:38:33 AM »
Well,
it was kinda fun to see the guy grab for his cell phone when I played his ringtone from my machine with the volume up. 
(about 6 of us in an open room divided with cubicals and music playing so it's not easy to tell where it's coming from).
He'll figure it out if I do it too much, so maybe I'll put it on a timer so it happens when I'm not at my machine or better yet,
when I'm over talking to him.  :-D