CLI Sound Player

1.1.3 · active · verified Wed Apr 22

cli-sound is a Node.js utility designed for playing audio files from command-line applications and other Node.js environments. As of version 1.1.3, it offers robust cross-platform compatibility by leveraging various locally installed, headless audio programs such as `ffplay`, `mpv`, or `mpg123`. It differentiates itself from similar packages like `play-sound` through enhanced reliability, native TypeScript types, comprehensive ESM and CommonJS support, and advanced features such as volume control and customizable audio player commands. The package operates by executing a compatible audio program found on the system via `node:child_process.exec`, automatically detecting the first available player from a predefined list. Its current release cadence appears stable, with a focus on incremental improvements and broad system compatibility.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to create a Player instance, configure it with options like volume, and play an audio file. Includes error handling for common issues like missing players.

import { Player } from 'cli-sound';
import { fileURLToPath } from 'url';
import path from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Provide a path to an actual audio file (e.g., MP3, WAV)
// Ensure you have an audio player like 'ffplay' or 'mpv' installed on your system.
// For demonstration, we'll use a placeholder. Replace with a real path.
const soundFilePath = process.env.SOUND_FILE_PATH ?? path.join(__dirname, 'test-sound.mp3');

async function playSound() {
  try {
    const player = new Player({
      volume: 0.5, // Play at 50% volume (if supported by player)
      // Optionally, specify custom commands or extend default ones
      // commands: ['ffplay -loglevel quiet -nodisp -volume %volume% %filepath%'],
    });

    console.log(`Attempting to play sound from: ${soundFilePath}`);
    await player.play(soundFilePath);
    console.log('Sound playback initiated.');
  } catch (error) {
    console.error('Failed to play sound:', error.message);
    console.error('Ensure you have a compatible audio player installed (e.g., ffplay, mpv) and the sound file path is correct.');
  }
}

playSound();

view raw JSON →