playsound

1.3.0 · maintenance · verified Fri Apr 17

playsound is a pure Python, cross-platform module designed for playing sounds with a single function call. It aims for simplicity and has no external Python dependencies. The current version is 1.3.0, released in 2021, indicating a stable but currently dormant release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to play a sound file using `playsound`. It dynamically creates a small, silent WAV file for cross-platform compatibility and then plays it. Replace `tmp_file_name` with the actual path to your sound file (e.g., `playsound('path/to/your/audio.mp3')`). Be aware that `playsound` blocks the execution of the script until the sound finishes, and specific audio formats may have platform-dependent support.

import os
import tempfile
from playsound import playsound

# Create a dummy WAV file for demonstration
# (playsound has no stop/pause, so a very short sound is ideal)
# In a real scenario, use an actual sound file like 'path/to/my_sound.wav'
# NOTE: For Windows, WAV is the most reliable format. MP3 support is limited.

try:
    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
        tmp_file_name = tmp_file.name
        # Minimal WAV header + 1 second of silent audio for demonstration
        # This is a very basic valid WAV header for a mono 16-bit 44.1kHz sound
        wav_header = b'RIFF\x2c\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00D\xac\x00\x00\x88\x58\x01\x00\x02\x00\x10\x00data\x08\x00\x00\x00\x00\x00\x00\x00'
        tmp_file.write(wav_header)
        tmp_file.flush()

    print(f"Playing sound from: {tmp_file_name}")
    playsound(tmp_file_name)
    print("Sound playback finished.")

finally:
    if 'tmp_file_name' in locals() and os.path.exists(tmp_file_name):
        os.remove(tmp_file_name)
        print(f"Cleaned up temporary file: {tmp_file_name}")

view raw JSON →