Play mp3 audio in Python
When we install Python on our windows system, it already contains the winsound module which will able to play sound. But it is only able to play sound with “.wav” extension and is not able to play MP3 sound. So, here in this tutorial, I am going to show you how to play mp3 audio in Python.
Play MP3 sound in Python using playsound module
In this tutorial, we are going to use the playsound library to play our audio MP3. So at the very first, we have to install the library. We can install it via pip:
$ pip install playsound
To know more about this library visit https://pypi.org/project/playsound/.
After we install the Python playsound module, we just have to import this library and use it just like you can see in the example below:
import playsound playsound.playsound('PATH_TO_MP3/sound.mp3', True)
That’s it, the above two lines of Python code will play the MP3 audio after you run it.
Below is the same Python program with another style that will work exactly the same like above:
from playsound import playsound playsound('audio/sound.mp3')
The above code will also play the mp3 file just like we did it before, but the code is slightly different. Here we have actually imported only the required part from the module. It is always recommended to use only the required part from the library.
There may be some other way to play an mp3 audio file, but I have found playing mp3 audio using playsound module is simpler than other ways.
I hope, you have also enjoyed this tutorial and understood what is discussed in this post.