How to play mp3 files in Python

Free Python Code - Aug 21 '23 - - Dev Community

Hi 🙂🖐

Today, I will share with you how to play MP3 files in Python.
You have many liblares in Python To play MP3 files.

1 - pygame

Install from here pip install pygame

Code


from pygame import mixer
import time

mixer.init()
mixer.music.load('Dirty Backseat - S.S.S (feat. AZZI).mp3')
mixer.music.play()

while mixer.music.get_busy():  # wait for music to finish playing
    time.sleep(1)
Enter fullscreen mode Exit fullscreen mode

2 - playsound

Install playsound from pip install playsound


from playsound import playsound

playsound('Dirty Backseat - S.S.S (feat. AZZI).mp3')

Enter fullscreen mode Exit fullscreen mode

3 - vlc

Install vlc from pip install python-vlc


import vlc
p = vlc.MediaPlayer("sample.mp3")
p.play()


Enter fullscreen mode Exit fullscreen mode

Now we're done 🤗

Don't forget to like and follow 🙂

Support me on PayPal 🤗
https://www.paypal.com/paypalme/amr396

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .