Skip to main content

Downloads Downloads Downloads

mpyg321

mpyg321 is a simple python wrapper for mpg321 and mpg123. It allows you to easily play mp3 sounds in python, do basic operations on the music and implement callbacks for events like the end of a sound.

Installation

mpyg321 requires the installation of mpg123 (or mpg321 depending on your usage) software for reading mp3. This section describes the installation of the library on MacOS, Linux and Windows.

We recommend using mpg123 since the project is more up to date. However, you can also use this library with mpg321 (using the MPyg321Player class)

MacOS

$ brew install mpg123 # or mpg321
$ pip3 install mpyg321

Linux

$ sudo apt-get update
$ sudo apt-get install mpg123 # or mpg321
$ pip3 install mpyg321

Windows

For windows installation, download mpg123 on the website: mpg123's website, and then run:

$ pip install mpyg321

Usage

Usage is pretty straight forward, and all the functionnalities are easily shown in the examples folder.

from mpyg321.MPyg123Player import MPyg123Player # or MPyg321Player if you installed mpg321
player = MPyg123Player()
player.play_song("/path/to/some_mp3.mp3")

Calbacks and Events

Callbacks

You can implement callbacks for several events such as: end of song, user paused the music, ... All the callbacks can be found inside the code of the BasePlayer class and the MPyg123Player class. Most of the callbacks are implemented in the callbacks.py example file.

Events

Starting from version 2.2.0, you can now subscribe to events using decorators and/or the subscribe_event function. Here is an example usage. You can find more details in the events.py example file.

player = MPyg123Player()

@player.on(MPyg321Events.ANY_STOP)
def callback(context):
    print("Any stop event occured")

# or
def my_func(context):
    print("Other event subscribed")

player.subscribe_event(MPyg321Events.ANY_STOP, my_func)

Here is an exhaustive list of the available events and their compatibilities with the different players (MPyg123 and MPyg321):

Event Description MPyg123Player MPyg321Player
USER_STOP When you stop the music (call player.stop) X X
USER_PAUSE When you pause the music (call player.pause) X X
USER_RESUME When you resume the music (call player.resume) X X
ANY_STOP When any stop occures (pause, stop, end of music) X X
MUSIC_END When the music ends X X
ERROR When an error occurs (The error info is within the context using the class MPyg321ErrorContext) X X
USER_MUTE When you mute the player X -
USER_MUTE When you unmute the player X -

Loops

In order to loop (replay the song when it ended), you can either set the loop mode when calling the play_song function:

player.play_song("/path/to/sample.mp3", loop=True)

or programmatically set the loop mode anywhere in the code:

player.play_song("/path/to/sample.mp3)
// Do some stuff ...
player.set_loop(True)

Note: when calling player.set_loop(True), the loop mode will only be taken into account at the end of a song. If nothing is playing, this call will not replay the previous song. In order to replay the previous song, you should call: player.play()

Metadata

Release files for mpyg321 2.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mpyg321 2.2.4
File Size Uploaded
mpyg321-2.2.4.tar.gz 9.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mpyg321 2.2.4
File Interpreter ABI Platform
mpyg321-2.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 18.9 kB

Release files / mpyg321-2.2.4.tar.gz

Download URL mpyg321-2.2.4.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
acb47de1a83a26a6d8646971d008242d00db04d05ff146b4d6838a0106f7a56c
BLAKE2b-256 checksum
How to use checksums
18060d283f98284f12ab82e96fa2152c851284cf4b1c2a6c0c146d2ec611dba1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / mpyg321-2.2.4-py3-none-any.whl

Download URL mpyg321-2.2.4-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
59365647fc771186ba5ad4511b1f323575dbb32103ff7ada5d500affe408f812
BLAKE2b-256 checksum
How to use checksums
b469ca779f74dc10ffc178cc4e2d1b19c0a36cb746370d86f459148697fa6d8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

2.2.4 This release

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.0

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page