🔊 Play sounds in Python scripts
minsound provides a simple cross-platform API to play sounds in Python scripts. It includes a synchronous API and an equivalent asynchronous API that is compatible with asyncio and trio.
For code examples, you can check out onhold and ding, or scroll down to the Usage section.
Why minsound?
boombox is great and 90% of the way there, however it is limited to only playing WAV files on Windows. playsound will play other formats than WAV on Windows, but it requires GStreamer and PyGObject bindings on Linux, while boombox has several playback backends for Linux other than, and including, GStreamer.
Neither boombox or playsound provide asyncio and async/await compatible APIs, but minsound does.
If you're targeting multiple desktop platforms and don't want to get mired down in the details of when and where to use playsound or boombox, or if your project uses async/await, you can just reach for minsound and call it a day.
Installation
$ python3 -m pip install minsound
Usage
This library uses pathlib.Path objects when pointing to filenames and paths. It can use aiopath.AsyncPath objects, too.
There's a synchronous API and an asynchronous API that you can use with the async/await syntax and asyncio.
Synchronous API
Play a file
from minsound import play_file, DEFAULT_SONG
play_file(DEFAULT_SONG) # blocks by default
# play without blocking
play_file(DEFAULT_SONG, block=False)
Play while work completes
from time import sleep
from minsound import play_while_running, DEFAULT_SONG
WAIT: int = 60
with play_while_running(DEFAULT_SONG):
sleep(WAIT)
Play a file after work completes
from time import sleep
from minsound import play_after, DEFAULT_SOUND
with play_after(DEFAULT_SOUND): # blocks by default
sleep(WAIT)
# play without blocking
with play_after(DEFAULT_SOUND, block=False):
sleep(WAIT)
Ring the terminal bell
from minsound import bell, bell_after
# play bell
bell()
# ensure the bell is played even if an exception is thrown
with bell_after():
raise Exception("Bye")
Asynchronous API
To run the following examples with top-level await expressions, launch an asynchronous Python REPL using python3 -m asyncio or an IPython shell.
Play a file
from minsound import play_file_async, DEFAULT_SONG
await play_file_async(DEFAULT_SONG) # blocks by default
# play without blocking
await play_file_async(DEFAULT_SONG, block=False)
Play while work completes
from asyncio import sleep
from minsound import play_while_running_async, DEFAULT_SONG
async with play_while_running_async(DEFAULT_SONG):
await sleep(WAIT)
Play a file after work completes
from asyncio import sleep
from minsound import play_after_async, DEFAULT_SOUND
async with play_after_async(DEFAULT_SOUND): # blocks by default
await sleep(WAIT)
# play without blocking
async with play_after_async(DEFAULT_SOUND, block=False):
await sleep(WAIT)
Support
Want to support this project and other open-source projects like it?
Copyright
See CREDIT.md.
License
See LICENSE.
Metadata
Release files for minimalsound 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| minimalsound-0.1.0.tar.gz | 7.8 kB | Without provenance | Details for minimalsound-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| minimalsound-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for minimalsound-0.1.0-py3-none-any.whl |
Total release size: 16.7 kB
Release files / minimalsound-0.1.0.tar.gz
| Download URL | minimalsound-0.1.0.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8eeea26a19e76117a304bb710a87801128e31f2d5f5262863bfe76b627265132
|
|
BLAKE2b-256 checksum How to use checksums |
81a974b4421f414292be3fe71fa4c1fd7510490f991d92dfec02b4b2acb359fb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.12
|
Release files / minimalsound-0.1.0-py3-none-any.whl
| Download URL | minimalsound-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e1eab162f3b3431990f6aaf8f1b3f32ce2c39d2b265df0af0bcf14dde06b728b
|
|
BLAKE2b-256 checksum How to use checksums |
3e5c8d45ee2ad8b76a86ed50614814cf249958bdc132122e52fbd4334aa3de72
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.12
|
Security for minimalsound 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.