How to Play Sound, Audio or MP3 Files in Python

How to Convert PNG to TIFF in Python?

If you’re trying to play, read, open or run sound, audio or MP3 files using Python, look no further, this is the perfect guide for you whether you’re on a Windows, Linux or macOS machine!

There are numerous ways for us to play sound, audio, or MP3 files using the Python programming language. These include modules such as playsound, VLC, pygame, mpg123, etc. We can also play it natively on macOS and Linux as we will see later on.

Most of the modules that we will be using don’t come pre-installed with Python, which means we will have to install them ourselves. Therefore, you need to have Python and PIP installed on your system, before installing these Python modules.

Luckily, playing audio in Python is straightforward and can be done with a few lines of code. In this guide, we will be going over the steps to play sound, audio, or MP3 files in Python on Windows, Linux, and macOS.

 

Note: In this guide, a 4-second MP3 file is used named testaudio.mp3 and it is located in the same directory as the .py file which we will be using to run the code.

 

 

Playing Sound, Audio, or MP3 Files in Python on Windows

Using Playsound

Playsound is a cross-platform Python module with no dependencies and a single function. 

So, to use playsound to play audio:

  1. First, enter the following command in the Command Prompt Terminal: 
pip install playsound

play, read, open or run sound, audio or MP3 files in Python on Windows using playsound with Command Prompt command

 

If you would like to double-check that you’ve properly installed the module, enter python in the terminal to access the python terminal and then import playsound. If playsound is installed, there should be no errors in the output. 

play, read, open or run sound, audio or MP3 files in Python on Windows using playsound with Command Prompt command

 

You can also enter pip list in the terminal to have a look at all the installed packages and their versions.

play, read, open or run sound, audio or MP3 files in Python on Windows using playsound with Command Prompt command

 

  1. In the code editor, enter the following:
import playsound

playsound.playsound('testaudio.mp3')

play, read, open or run sound, audio or MP3 files in Python on Windows using playsound

 

  1. After running the code, your audio will begin playing and will stop automatically depending on the length of your mp3 file.

 

Using VLC

VLC is an open-source and cross-platform media player that can be used to play audio and video files. VLC has a python module that enables us to play mp3 files. 

Hence, to use VLC to play audio:

  1. First, enter the following command in the Command Prompt Terminal
pip install python-vlc

play, read, open or run sound, audio or MP3 files in Python on Windows using vlc

 

Confirm the installation by entering:

pip list

play, read, open or run sound, audio or MP3 files in Python on Windows using vlc

 

  1. We will be using another module alongside the Python VLC module in order to delay the execution of the file. The time module enables us to determine how long we want the file to delay execution so we can hear our audio being played. Otherwise, the file will finish executing immediately without our audio being played.
  2. In your code editor, enter the following:
import  vlc

import time



p = vlc.MediaPlayer(‘testaudio.mp3’)

p.play()

print("The audio will finish playing in 4 seconds")

time.sleep(4)

p.stop()

play, read, open or run sound, audio or MP3 files in Python on Windows using vlc

 

The time.sleep(4) function tells the program to delay execution for 4 seconds before we call the p.stop() function. When running the program, it should look something like this: 

play, read, open or run sound, audio or MP3 files in Python on Windows using vlc

 

A terminal window will appear and your audio should start playing. At the same time, the result of the print function is shown to indicate that the audio is 4 seconds long. The program will finish executing right after as that is the time we specify in time.sleep(4).

 

Using Pygame

Pygame is a game development module that is cross-platform and enables us to use Python for graphics and audio purposes. 

To use pygame to play audio:

  1. First, enter the following command in the Command Prompt Terminal:
pip install pygame

play, read, open or run sound, audio or MP3 files in Python on Windows using pygame

 

  1. Second, enter the following in your code editor:
import pygame

import time



pygame.mixer.init()  # initialize mixer module

pygame.mixer.music.load('testaudio.mp3')

pygame.mixer.music.play()

print("Audio will play for 4 seconds")

time.sleep(4)

play, read, open or run sound, audio or MP3 files in Python on Windows using pygame

 

The result will look like this:

play, read, open or run sound, audio or MP3 files in Python on Windows using pygame

 

First, the audio will start playing, and the result of the print function will be shown on the screen. Then, the program will finish executing after 4 seconds as specified by the time.sleep() function.

 

Playing Sound, Audio, or MP3 Files in Python on Linux

Using Playsound

  1. First, enter the following in the Terminal
pip3 install playsound

play, read, open or run sound, audio or MP3 files in Python on Linux using playsound

 

  1. Next, enter the following piece of code:
import playsound

playsound.playsound(‘testaudio.mp3’)

play, read, open or run sound, audio or MP3 files in Python on Linux using playsound

 

  1. Lastly, run the code and your audio will begin playing.

play, read, open or run sound, audio or MP3 files in Python on Linux using playsound

 

Using os and mpg123

os is a standard Python module that doesn’t need to be installed. It allows us to interact with our Operating System through its functions. mpg123 is an open-source mp3 player for Linux systems.

So, we can use methods from both these functions to play audio files with Python through the following steps:

  1. First, install mpg123 by entering the following in the Terminal
sudo apt install mpg123

 

  1. Next, you can verify that the package is installed by entering: 
dpkg –l mpg123

play, read, open or run sound, audio or MP3 files in Python on Linux using mpg123

 

  1. Next, enter the following piece of code:
import os

os.system(‘mpg123 ‘ + ‘testaudio.mp3’)

play, read, open or run sound, audio or MP3 files in Python on Linux using mpg123

 

  1. Lastly, run the code and your audio will begin playing.

play, read, open or run sound, audio or MP3 files in Python on Linux using mpg123

 

Playing Sound, Audio, or MP3 Files in Python on macOS

Using Afplay and os

To play audio on a macOS-based machine, we will be using afplay and os. Afplay is a tool that allows us to play audio from a file on macOS. 

  1. First, enter the following piece of code:
import os

os.system(“afplay ” + “testaudio.mp3”)

 

  1. Next, run the code and your audio should begin playing.

 

Conclusion

Learning how to play audio, sounds, and MP3 files in Python can be quite useful in many circumstances. There are a number of reasons why someone may want to play music or audio in Python. 

These include wanting to add to their programming skills, adding music to a game, adding sound effects to an app, and many more!

With that said, in this article, we looked at the steps to play sound, audio, or MP3 files in Python on Windows, Linux, and macOS machines. We hope you’ve found this guide helpful when it comes to playing audio in Python.

Feel free to share this post with your fellow coders to guide them through playing sound, audio, or MP3 files with Python on Windows, Linux, and macOS!

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share