A lightweight, cross-platform Python library for playing background music in CLI tools without blocking.
Project description
ez_background_music 🎵
A lightweight, cross-platform Python library designed for playing background music in CLI (Command Line Interface) tools and Terminal applications without blocking your code execution.
Why ez_background_music?
Many Python audio libraries (like the original playsound) block the execution of your script until the song is finished. While some support asynchronous play, they often "choke" or fail to start when the terminal is busy with heavy ASCII animations, keyboard listeners, or screen clears.
ez_background_music is built to be "bulletproof" for terminal apps:
- Truly Non-blocking: Launches audio as a separate system process.
- Terminal-Optimized: Won't cut out when you clear the screen or run intensive
printloops. - Cross-Platform: Uses native macOS
afplay, Linuxpaplay, and Windowsmultiprocessingfor maximum stability.
Installation
pip install ez_background_music
Usage Examples
1. Basic "Fire and Forget"
Perfect for adding a theme song to your script's intro.
from ez_background_music import start_music, stop_music
import time
# Start the music (returns True if successful)
start_music("intro_theme.mp3")
print("The music is playing in the background!")
# Your code continues immediately
time.sleep(5)
# Stop the music
stop_music()
2. Integration with a CLI Loop
If you are building an interactive tool (like a GPR Reader or a Game), use this pattern to ensure the music stops when the user exits.
import sys
from ez_background_music import start_music, stop_music
def main():
# Start background music at the beginning
start_music("ambient_track.wav")
print("Welcome to My App!")
while True:
user_input = input("\n> ").strip().lower()
if user_input == "exit":
print("Cleaning up...")
stop_music() # Essential to stop the process
sys.exit()
else:
print(f"You entered: {user_input}")
if __name__ == "__main__":
main()
How it Works
The library detects your operating system and chooses the most "isolated" way to play sound so your main Python script stays fast:
| OS | Method | Benefit |
|---|---|---|
| macOS | Native afplay |
Zero CPU impact; works even if Python hangs. |
| Windows | multiprocessing (with playsound3) |
Bypasses the GIL to prevent audio stutter. |
| Linux | paplay (fallback to multiprocessing) |
Uses native sound servers, with a robust fallback. |
Troubleshooting
Absolute vs. Relative Paths
If the music doesn't play, ensure you are providing the correct path. It is always safer to use absolute paths:
import os
from ez_background_music import start_music
# Get the directory where your script is located
base_path = os.path.dirname(__file__)
audio_path = os.path.join(base_path, "music.mp3")
start_music(audio_path)
macOS Permissions
If running in a highly restricted terminal environment, ensure the Terminal has "Accessibility" permissions if you are also using libraries like keyboard.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ez_background_music-1.0.0.tar.gz.
File metadata
- Download URL: ez_background_music-1.0.0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
99392c6571a3d189114c9db75555671892ea166a8dea00c28c3d012e7f176e4a
|
|
| MD5 |
b51471c67ffea62995bee340720e249f
|
|
| BLAKE2b-256 |
862ec7e6f2b90ac26ea1fbb3780d330fd539cb27f10b25032af66e03954b60e4
|
File details
Details for the file ez_background_music-1.0.0-py3-none-any.whl.
File metadata
- Download URL: ez_background_music-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5360858e806d7ca8458cbaff34f0001fa57c3b7c6517bad4b3278b066c305b12
|
|
| MD5 |
14f15d2b76949d66e525f61fcda1eda9
|
|
| BLAKE2b-256 |
d77fd7ffdd1cae5fe1861bb965af35158da3611809607802be0454c3cdef6b27
|