Skip to main content

🔊 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. 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 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)

Ring the terminal bell

from play_sounds 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 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?

Buy Me A Coffee

Copyright

See CREDIT.md.

License

See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

play_sounds-0.5.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distributions

play_sounds-0.5.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

play_sounds-0.5.0-py2.py3-none-any.whl (7.5 MB view details)

Uploaded Python 2 Python 3

File details

Details for the file play_sounds-0.5.0.tar.gz.

File metadata

  • Download URL: play_sounds-0.5.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for play_sounds-0.5.0.tar.gz
Algorithm Hash digest
SHA256 0f06536a125e0fcbbe97fbf17101a8de5019d523265c0d988a0cd4cca16894ba
MD5 f3077f9e11469661e64fc95c13121977
BLAKE2b-256 069c11d1b327094091a99f09a058a34d6cedb486a7d52205bd0f8bd2fde23567

See more details on using hashes here.

Provenance

File details

Details for the file play_sounds-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: play_sounds-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for play_sounds-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f64692e2ff644d1b6e8f5f73c13b708c879bc79033e7f6561afa793336e4a50
MD5 a16ef3a194853a2dc52a628130883e74
BLAKE2b-256 a539fe8b5108434f7bb80eb6e3e3620575ca5a8f5e5827d77e0b0cc5483e9bff

See more details on using hashes here.

Provenance

File details

Details for the file play_sounds-0.5.0-py2.py3-none-any.whl.

File metadata

  • Download URL: play_sounds-0.5.0-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.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for play_sounds-0.5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0f512ea2f4e49a0b3d73ecd571d4fc6c6d8091b18d80478d13526618319ac1fd
MD5 7dfe548c5e43977704e7efa73be39150
BLAKE2b-256 134937d4d8ef1c7d418a73280703b96fe4c775661d5ece61ecf00ed4a853bccc

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page