🔊 Play music and sounds in your Python scripts
Project description
🔊 Play sounds in your Python scripts
play_sounds
provides a simple cross-platform API to play sounds in your Python scripts, and allows you to play sounds while a function or code block executes, or afterwards. The library 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 the default options for playing sounds on Windows are limited to WAV files. If the platform is Windows, play_sounds
will default to the playsound
backend.
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
Playing 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)
Playing 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
.
Playing 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)
Playing while work completes
from asyncio import sleep
from play_sounds import play_while_running_async, DEFAULT_SONG
async with play_while_running(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
Hashes for play_sounds-0.3.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f30fda7717b3c22ae556494e4a4b5aff0cc452a4692ca7ef2bc29db2497e0b6 |
|
MD5 | 01193856b4a25833ba0200a4720b8db1 |
|
BLAKE2b-256 | 1c0497355a9413ded4b8d7f6e940a50f983225a61f898a170883b41de3fd541b |