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

Neither libraries provide asyncio and async/await compatible interfaces, but play_sounds does.

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(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.4.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: play_sounds-0.4.1.tar.gz
  • Upload date:
  • Size: 4.9 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

Hashes for play_sounds-0.4.1.tar.gz
Algorithm Hash digest
SHA256 71bfa9c2e29b3ac5fa9aee1c036443a9e25ef005f427d1efec8d925cdfa7a739
MD5 def4e7195e0034c2a72b81083aa8da6d
BLAKE2b-256 1a341faa8b328885422ac4a98a6da45bfb261fcc56afd733e1d8fce57870172a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: play_sounds-0.4.1-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

Hashes for play_sounds-0.4.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9663f801b4708852d5c3fb00e1dded7ac7ff3c01907dba810f90e9208875987c
MD5 f5425f15ed8e965d7086036c5c302767
BLAKE2b-256 13a203991db94666d601f7199624ad8bcb7b043749bc882c2d210f9d59bc6aee

See more details on using hashes here.

Supported by

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