UNPKG

node-windows-audio-manager-switcher

Version:
310 lines (206 loc) β€’ 9.22 kB
# 🎧 Node Windows Audio Manager > Native Node.js addon for managing **Windows audio playback devices** > Powered by **C++ + Node-API (N-API)** and ships with **prebuilt binaries** for easy plug-and-play. --- ## πŸš€ Features - πŸ” List all active audio output devices (name, ID, isDefault) - 🎚️ Set any device as the system's default playback device - πŸ”‡ Mute / unmute: - βœ… Default output device - βœ… Any specific device (by ID) - βš™οΈ Built with Windows Core Audio + COM API - πŸ’‘ Prebuilt `.node` binaries β€” **no build tools required** --- ## πŸ“¦ Installation ```bash npm install node-windows-audio-manager-switcher ``` βœ… Works out of the box on Windows βœ… No Visual Studio or compilation needed > πŸ’¬ If you modify native C++ code, see [Build From Source](#prebuilt-native-addon) --- ## 🧠 Usage Examples ### πŸ” List Playback Devices ```js const { listDevices } = require('node-windows-audio-manager-switcher'); const devices = listDevices(); devices.forEach((device, index) => { console.log(`${index + 1}. ${device.name} [${device.id}] Default: ${device.isDefault}`); }); ``` --- ### 🎚️ Set Default Playback Device ```js const { listDevices, setDefaultDevice } = require('node-windows-audio-manager-switcher'); const devices = listDevices(); const target = devices.find(d => d.name.includes("Speakers")); if (target) { const success = setDefaultDevice(target.id); console.log(success ? "βœ… Set as default!" : "❌ Failed to set device."); } ``` --- ### πŸ”‡ Mute / Unmute Default Device ```js const { setDefaultPlaybackMute } = require('node-windows-audio-manager-switcher'); setDefaultPlaybackMute(true); // Mute setDefaultPlaybackMute(false); // Unmute ``` --- ### πŸ”‡ Mute / Unmute Specific Device by ID ```js const { listDevices, muteDeviceById } = require('node-windows-audio-manager-switcher'); const target = listDevices()[0]; // Example: first device muteDeviceById(target.id, true); // Mute muteDeviceById(target.id, false); // Unmute ``` --- ## πŸ“˜ API Reference | Function | Description | |----------|-------------| | `listDevices()` β†’ `{ name, id, isDefault }[]` | Lists all active output devices | | `setDefaultDevice(deviceId)` β†’ `boolean` | Sets the default playback device | | `setDefaultPlaybackMute(mute)` β†’ `boolean` | Mute/unmute the default device | | `muteDeviceById(deviceId, mute)` β†’ `boolean` | Mute/unmute a specific device | --- ## πŸ“‚ Project Structure ```bash node-windows-audio-manager-switcher/ β”œβ”€β”€ index.js # JS bindings to native addon β”œβ”€β”€ native/ # C++ source code (AudioSwitcher, DeviceUtils) β”œβ”€β”€ prebuilds/ # Precompiled binaries (.tar.gz) β”œβ”€β”€ build/ # Generated at install (addon.node) β”œβ”€β”€ test/ # Interactive example scripts └── binding.gyp # node-gyp config file ``` --- ## πŸ§ͺ Dev Commands ```bash # Build from source (dev) npm run dev:build # Generate prebuilt binary (for npm publish) npm run dev:prebuild # Run example tests npm run dev:test:devices npm run dev:test:set-default npm run dev:test:mute-default npm run dev:test:unmute-default npm run dev:test:mute-device npm run dev:test:unmute-device ``` --- ## Prebuilt Native Addon No toolchain? No problem. We use [`prebuild`](https://github.com/prebuild/prebuild) to compile and package `.node` binaries: ```bash npx prebuild --backend=node-gyp -t 20.13.1 --strip --napi ``` At install, `prebuild-install` downloads the correct binary from the `prebuilds/` folder. > πŸ“¦ Works seamlessly for Windows x64 and Node.js 20.x+ --- ## ⚠️ Troubleshooting | Problem | Solution | |----------------------------------------------|--------------------------------------------------------------------------------------------| | `node_api.h` not found | Make sure `node-addon-api` is installed: `npm install node-addon-api` | | `addon.node` missing | Run `npm rebuild` or `npm run dev:build` | | `CoCreateInstance` fails | Run your script with **Administrator privileges** (Right click > "Run as Administrator") | | `prebuild` fails | Ensure Node.js version matches and all required build tools are installed | | ❌ `WindowsTargetPlatformVersion` error | See **"Missing Windows SDK version" fix below** | --- ### πŸ› οΈ Missing Windows SDK Version You might see an error like: ```bash The Windows SDK version 10.0.22621.0 was not found. Install the required version of Windows SDK or change the SDK version... ``` This happens when your system **doesn't have the specific Windows SDK version** your build setup is looking for. #### βœ… Solution 1: Let it use the latest installed SDK (Recommended) Update your `binding.gyp` file and **remove the hardcoded SDK version**: **Before (❌):** ```json "msvs_windows_target_platform_version": "10.0.22621.0" ``` **After (βœ…):** Just delete that line entirely. Then rebuild everything: ```bash npx node-gyp clean npx node-gyp configure npx node-gyp build ``` Node-gyp will now use **the latest installed SDK version** automatically. --- #### βœ… Solution 2: Download the required SDK version If you **want to use a specific SDK version** mentioned in the error (like `10.0.22621.0`), do this: 1. Visit the official Windows SDK archive: πŸ‘‰ [https://developer.microsoft.com/en-us/windows/downloads/sdk-archive/](https://developer.microsoft.com/en-us/windows/downloads/sdk-archive/) 2. Download and install the **exact SDK version** shown in the error. 3. Once installed, re-run: ```bash npm install ``` --- ### πŸ” How to check installed SDKs Run this in PowerShell: ```powershell Get-ChildItem 'C:\Program Files (x86)\Windows Kits\10\Include' | Select-Object Name ``` You’ll see something like: ```bash Name ---- 10.0.19041.0 10.0.26100.0 ``` Use one of these versions if you want to manually set it. --- ### πŸ“Œ Note on Prebuilt Binaries This package ships with **prebuilt native binaries** for seamless installation β€” no C++ compiler, Visual Studio, or Windows SDK required! βœ… Just run: ```bash npm install node-windows-audio-manager-switcher-switcher ``` > Under the hood, we use [`prebuild`](https://github.com/prebuild/prebuild) + [`prebuild-install`](https://github.com/prebuild/prebuild-install) to provide platform-specific `.node` binaries. #### ℹ️ How It Works - During `npm install`, `prebuild-install` checks for a compatible `.tar.gz` binary (based on your Node.js version and OS). - If a matching binary is found (e.g. Node.js 20.x, Windows x64), it downloads and installs automatically. - If **no matching binary** is available, it falls back to building from source β€” which requires **Visual Studio + Windows SDK**. Here’s the updated section with a clean **batch-style** note and a list of supported versions so far: --- #### πŸ’‘ Supported Node Versions We currently provide prebuilt native binaries for the following Node.js versions: | Node.js Version | ABI Version | Status | |-----------------|-------------|------------| | βœ… 22.x | `v127` | Supported | | βœ… 21.x | `v130` | Supported | | βœ… 20.x | `v115` | Supported | | βœ… 19.x | `v111` | Supported | | βœ… 18.x (LTS) | `v108` | Supported | > πŸ’‘ **Note:** If you're using a different version of Node.js and encounter the message > `prebuild-install warn install No prebuilt binaries found`, you can either: > > 1. **Switch to a supported version** (recommended), or > 2. **Manually build from source** by running: ```bash npm install --build-from-source ``` - _(More coming soon...)_ --- If you're using a newer Node.js version and see an install error like: ```bash prebuild-install warn install No prebuilt binaries found (target=22.14.0 ...) ``` This means we haven't published a prebuilt binary for your Node version **yet**. You can either: - βš™οΈ Build from source (requires build tools), or - 🧩 Downgrade to a supported Node.js version (e.g. Node 20.x) --- ## πŸ™Œ Contributing We welcome PRs and ideas! You can contribute by: - 🎚️ Adding volume control APIs - πŸ” Listening to real-time device change events - βœ… Writing unit tests (e.g. with `jest` or `mocha`) --- ## ❀️ Credits - Microsoft Core Audio APIs (`IMMDevice`, `IPolicyConfig`, `IAudioEndpointVolume`) - [node-addon-api](https://github.com/nodejs/node-addon-api) (N-API C++ bindings) - Inspiration: [tartakynov/audioswitch](https://github.com/tartakynov/audioswitch)