Play mp3 audio file using Python

Details

Play mp3 audio file using Python

1. Python module "playsound"

Its a great module to play sound files. Its cross platform that means it works on Windows, Mac as well as Linux.

Refer for more details: https://pypi.org/project/playsound/

Install playsound

pip3 install playsound

2. Code to play mp3 

from playsound import playsound
file = "song.mp3"
playsound(file, True)

3. Dealing with Issues

Issue 1. File load issue

Any space in the absolute path of file will cause issue and will throw IOError.

A simple fix is to replace all the spaces with %20.

from playsound import playsound
file = "<absolute-path>/song.mp3"
file = file.replace(" ", "%20")
playsound(file, True)
True (its default) flag passed to playsound function will block the code. If passed False file will be played in async mode. If using False i.e. aync mode make sure your Python code doesn't exit.

Issue 2. No module named 'AppKit' - Mac Issue

To resolve this install PyObjC

pip3 install -U PyObjC