. Advertisement .
..3..
. Advertisement .
..4..
There are various ways to play sound, MP3, and audio using Python. Follow us to see how to play MP3 file using python on Windows, Linux, MacOS machines.
How To Play MP3 File Using Python
Play MP3 File In Python On Windows
Method 1: Use Playsound
This cross-platform Python module comes with a single function and no dependencies. Activate to use the pip install playsound command in the Command Prompt Terminal.
import playsound
playsound.playsound(‘testaudio.mp3’)
After running this code, your audio starts playing and stops automatically depending on your file’s length.
Method 2: Use VLC
You can use this cross-platform and open-source media player to play your favorite video and audio files. Start the process by installing the vlc.py module first:
pip install python-vlc
Enter the pip list to confirm the installation step.
Input to play MP3 file:
import vlc
import time
example_mp3 = vlc.MediaPlayer('How To Play MP3 File Using Python.mp3')
example_mp3.play()
print("The audio will finish playing in 11 seconds")
time.sleep(11)
example_mp3.stop()
Method 3: Use Pygame
Pygame is also known as a game development module enabling users to use Python for audio and graphics purposes. To make use of this platform, enter pip install pygame in the Command Prompt Terminal.
Then type the following code to play the music:
import pygame
import time
pygame.mixer.init()
pygame.mixer.music.load('How To Play MP3 File Using Python.mp3')
pygame.mixer.music.play()
print("Audio will play for 11 seconds")
time.sleep(11)
Play MP3 File In Python On Linux
There are two methods to play audio files on Linux:
Method 1: Use Playsound
Like playing sounds in Windows, you can also use the playsound platform to enjoy your MP3 files. Start by entering pip3 install playsound in the Terminal to activate it.
Next:
import playsound
playsound.playsound('How To Play MP3 File Using Python.mp3')
In [19]: runfile(‘ /home/emaan/ .config/spyder-py3/ temp.py’, wdir= ‘/home/emaan/Documents/Python stuff’)
Method 2: Use OS And MPG123
You don’t have to install the OS, as it is a standard module in Python. This one lets users interact with the Operating System easily. On the other hand, mpg123 is a MP3 player for all Linux systems.
Use both function to play audio by following steps:
- Step 1: Install by typing sudo apt install mpg123
- Step 2: Verify the package with dpkg –l mpg123
- Step 3: Run the code to play your MP3 file
import os
os.system('mpg123 ' + 'How To Play MP3 File Using Python.mp3')
Play MP3 File In Python On MacOS
With macOS, you can use Afplay and os, which is a common tool to play audio from a file. The steps are easy to follow. All you have to do is run the command:
import os
os.system("afplay " + "How To Play MP3 File Using Python.mp3")
Conclusion
Learning how to play MP3 file using Python is useful in various cases. It allows you to add music to your game, sharpen your programming skills, or add sound effects to a developing app.
This tutorial offers easy methods to play sound on Windows, Linux, and MacOS systems. Thus, check it to have your job done.
Leave a comment