File Exchange

image thumbnail

SoundVolume - set or get the system speaker sound volume

version 1.2.0.0 (4.51 KB) by Yair Altman
SoundVolume sets or gets the computer system's speaker sound volume, using Java

9 Downloads

Updated 09 May 2014

View License

SoundVolume sets or gets the computer's system speaker sound volume

Syntax:
volume = SoundVolume(volume);

SoundVolume(volume) sets the system speaker sound volume. The volume value should be numeric, between 0.0 (=muted) and 1.0 (=max).

volume = SoundVolume gets the current speaker sound volume (0.0-1.0).

volume = SoundVolume(volume) sets the system speaker sound volume and returns the previous volume value (from before setting the new value).

Note:
SoundVolume relies on Java functionality that is fully supported and documented. In this respect, SoundVolume is different from some of my other Matlab-Java utilities that rely on undocumented code.

Bugs and suggestions:
Please send to Yair Altman (altmany at gmail dot com)

Technical description:
http://UndocumentedMatlab.com/blog/updating-speaker-sound-volume
http://java.sun.com/docs/books/tutorial/sound

Comments and Ratings (22)

Can one use it for controling the mono Sound so basically changing Sound value to the left and the Right earphones volume seperatly?

Doesn't work for me

Marius Rusu

Hi, I am running Windows 7 Professional 64 bits and it didn't work for me. The code runs without any errors. However, it seems to adjust a different volume on my PC. For example if my master volume is 50% and I call the program with an input argument of 0, the master volume is still 50% but I don't hear anything if I play a sound form Matlab. If I set to 0.5 then I head around 25% of the max volume. If I set to 1 then I head 50% of the max volume. In all cases the main volume in the lower right corner always shows 50%. So it seems to add another volume on top of the master volume.
I used nirsoft (how ankur gupta suggested) and that did worked indeed. That changed the master volume.

sawa0258

Hi, This is exactly what I need, however it's not 100% doing what I need. I'm using Windows 10, so that might be the cause of the issue.

It changes the Matlab specific output for my speakers, but a value of 1 just sets it to the current speaker level and 0.5 to half the current speaker level. I need to be able to change the overall speaker level.
Any idea how I can address the speaker itself instead of the Matlab value?
Any input is hugely appreciated!

It works fine for me :)
I tried it with a wav file and set the volume with a slider using your code.

thanks!

ankur gupta

The given code does not work. However, nircmd (http://www.nirsoft.net/utils/nircmd.html) works like a charm.
Dump the nircmd in matlab directory

system('nircmd.exe setsysvolume 65535'); % for max volume

system('nircmd.exe setsysvolume 0'); % for min volume

NKakon

did this work for anyone on windows 8.1?
I tried using it and it was able to change the value of volume but the actual volume coming out of the computer was the same...

Amad Mumtaz

Can any one please tell me that how can i used this code?? i need to control wind media player or system volume using Matlab.
I am using Matlab r2013a windows 7
rep ASAP

Leo

Hi all.
I've tried it in windows 7 and no problems detected (code run till the end, no warnings) It says the volume has been changed, though it remains actually unchanged! Any tip? Thank you.

Yair Altman

The latest update should hopefully solve the issue with Windows Vista, 7 & 8

Evan

Hi All.
Does this function work on WIN8? I'm trying to adjust the system volume in a GUI as part of a school project. I haven't been able to find a way to change or set the system volume through Matlab on WIN7 & WIN8. I am looking for a solution that won't require the user to change settings on their computer.

Thank you.

Evan

Yair Altman

Windows7 prevents applications from modifying the master volume control. It only works on XP.

nellaham

Yair. Thanks for a much needed utility. I can't wait to rate it.
Have you figured out a solution to get this to work on Windows 7? Seems like a few others have reported this problem. I traced through and found that the code finds the right controls, but it seems like the following calls have no effect on the volume setting:
ctrl.getValue
ctrl.setValue(volume)

Are you aware of any Windows 7 setting that could be preventing these calls from returning or affecting the value of the volume control. Your help would be much appreciated. Thanks

Florian

Does this work in Windows 7? I tried with and without D Nguyen's modification without success.

Reid

I've had the same issue, but found a way around it.

In windows, open regedit, and select current user>control panel>sounds. Then select beeb and 'modify'. Where it says 'yes', type 'no' without quotes. Then just restart the computer. This turns off the beep for all applications, which works great for me, but may not be what you want.

Trevor Agus

anyone knows the steps of this process?

it doesn't work on my computer.. I want to control volume by using matlab but it didn't..

im using Matlab R2009b and Windows Vista..

can anyone help me?

I want to do these functions for my final year project
and i have been searching for so long
finally found this code but it is giving this error when executed

??? Undefined command/function 'toChar'.

Error in ==> opaque.char at 94
chr = toChar(opaque_array(1));

Error in ==> SoundVolume at 83
if ~isempty(strfind(lower(char(ctrls(ctrlIdx).getType)),'volume'))

:((
plz help me..

Jay Casey

Yes, works perfectly but with the edition D Nguyen suggested! Thanks a lot!

D Nguyen

I used this to integrate it with a GUI slider to control the system volume. Worked great! Had to slightly modify te code..
ctrls(ctrlIdx).getType to ctrls(ctrlIdx).getType.toString..with Yair Altman's help to get it to work on my system. Windows XP with MATLAB 7.0.4.365(R14) SP2, but otherwise works fine!

Updates

1.2.0.0

Fix for Windows Vista, 7 and hopefully also 8

1.1.0.0

Clarified that SoundVolume uses fully documented and supported Java functionality.

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags
Acknowledgements

Inspired: MEAP - Matlab Enhanced Audio Player