🔊 Play music and sounds in your Python scripts
Project description
🔊 Play sounds in Python scripts
play_sounds
provides a simple cross-platform API to play sounds in Python scripts. It includes a synchronous API and an equivalent asynchronous API.
For code examples, you can check out onhold
and ding
, or scroll down to the Usage section.
Rationale
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 play_sounds
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 play_sounds
and call it a day.
Installation
python3 -m pip install play_sounds
Usage
This library uses pathlib.Path
objects when pointing to filenames and paths.
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 play_sounds 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 play_sounds import play_while_running, DEFAULT_SONG
with play_while_running(DEFAULT_SONG):
sleep(60)
Play a file after work completes
from time import sleep
from play_sounds import play_after, DEFAULT_SOUND
with play_after(DEFAULT_SOUND): # blocks by default
sleep(60)
# play without blocking
with play_after(DEFAULT_SOUND, block=False):
sleep(60)
Asynchronous API
To run the following examples with top-level await
expressions, launch an asynchronous Python REPL using python3 -m asyncio
.
Play a file
from play_sounds 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 play_sounds import play_while_running_async, DEFAULT_SONG
async with play_while_running_async(DEFAULT_SONG):
await sleep(60)
Play a file after work completes
from asyncio import sleep
from play_sounds import play_after_async, DEFAULT_SOUND
async with play_after_async(DEFAULT_SOUND): # blocks by default
await sleep(60)
# play without blocking
async with play_after_async(DEFAULT_SOUND, block=False):
await sleep(60)
Support
Want to support this project and other open-source projects like it?
Copyright
See CREDIT.md
.
License
See 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
File details
Details for the file play_sounds-0.4.3.tar.gz
.
File metadata
- Download URL: play_sounds-0.4.3.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1a6eaf5c5e06a725eb3d9837085b0769d8ca0a107eee18273ee63cb092a899d |
|
MD5 | 05ccd7ce4cec00df56c010da3bf2f44a |
|
BLAKE2b-256 | 8cb86d680352f3e6ec6156e2596ce7543f08aeddd1f26261f3b4d21483080687 |
Provenance
File details
Details for the file play_sounds-0.4.3-py2.py3-none-any.whl
.
File metadata
- Download URL: play_sounds-0.4.3-py2.py3-none-any.whl
- Upload date:
- Size: 7.5 MB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 656330952994de99b5f079ea9c872b5b31eaae02bb2f89c79b721b153a4ce7de |
|
MD5 | 69ad324cfa08cf0ec003581c1032466d |
|
BLAKE2b-256 | e69c754c30e4908e0867296298b49706304bf2dcb6038b47dea2b05b4f4ea0e7 |