Controlling system volume level in Node.jshas never been easier!
Thiscross-platform,dependency-free library lets youcontrol and get the status of volumein your system.No matter what OS or what framework(it works everywhere - as well as on regular Node.js and Electron - where they were tested), you don't have to worry about the compatibility and potential errors - this library willhandle it all.
Note that this library solves the Electron production-build specific problem with asar-packed files - other libraries similar to this, which have to call executable file or native module, don't work on Electron -easy-volume, on the other hand, was designed (and tested) to work with Electron like a charm!
import{setVolume,getVolume,setMute,getMute}from"easy-volume";
// Set volume - value from 0 to 100%
awaitsetVolume(20);
// Get volume - value from 0 to 100%
constvolume:number=awaitgetVolume();
console.log(volume);// 20
// Set mute - true is muted and false is unmuted
awaitsetMute(true);
// Get current mute status - true if system audio is muted, otherwise false
constisMuted:boolean=awaitgetMute();
console.log(isMuted);// true
getVolume(): Promise<number>
- Get current system volume
- Returns:System volume, from 0 to 100 [%]
setVolume(targetValue: number) => Promise<void>
- Change system volume to target value
- ParamtargetValue:Target volume, from 0 to 100
getMute(): Promise<boolean>
- Get current mute status (whether the system audio is muted or not)
- Returns:Whether the system audio is muted, i.e. true == muted, false == unmuted
setMute(isMuted: boolean) => Promise<void>
- Either mute or unmute system audio
- ParamisMuted:Whether to mute or unmute the system audio
toggleMute() => Promise<void>
- Toggle mute state
- Returns:Current (new) mute state (true == muted, false == unmuted)
- Windows(usesnative C++ CLI toolI wrote by myself (see
src/platforms/windows/main.cpp
)) - macOS(usesAppleScript (osascript))
- Linux(usesAdvanced Linux Sound Architecture (ALSA, amixer)- installed by default on most Linux distros)
- Windows 11 22H2
- macOS Ventura 13.4
- Ubuntu 22.04.2 LTS
You can test the library on your setup by running this command:
npmtest
If any of the test fails or you're using another setup which isn't implemented here, feel free to create an issue or a pull request.
Feel free to give some ideas for future features by creating issues on theGitHub repository.
To build the library, just run simple
npm run build
IMPORTANT:After building, be sure to copysrc/platforms/windows/volume.exe
intodist/platforms/windows/volume.exe
If you're using a different setup and/or want to create your own implementation of the library, feel free to make a pull request.
- In
src/platforms
,create a new directory with the name of your target platform. - In your newly created directory, create index.ts file which exports an object of type
PlatformImplementation
(see its declaration insrc/types.ts
). - Create your custom implementation. Note: if you're calling some native modules or any files, make sure to surround your path with the
toElectronPath
function fromsrc/utils/toElectronPath.ts
. - In the
src/index.ts
file, add a special case to theswitch
statement with your platform, following the patterns in the file. - Before creating the pull request, please test the library (see above for running tests).
Made with ❤️ byArtur Nowak