Play mp3 audio in Python
When we install Python on our windows system, it already contains the winsound which will able to play sound. But it 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
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 library, we just have to import this library and us it just like you can see below:
import playsound playsound.playsound('PATH_TO_MP3/sound.mp3', True)
That’s it, the above two lines of code will play the MP3 audio after you run it.
Below is the same code with another style that will work exactly the same:
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.
There may be some other way to play an mp3 audio file, but I have found play mp3 audio using playsound library is simpler than other ways.
I hope, you have also enjoyed this tutorial and understood what is discussed in this post.