Open In App

Play Sound in Python

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will explore how to play sound in Python using some of the most popular audio libraries. Whether you need to play a simple sound effect or work with complex audio files, these methods will cover your needs.

We'll discuss five different approaches to play sound in Python, using modules like playsound, pydub, tksnack, and more. You'll also find examples to make implementation easier.

Why Play Sound in Python?

Playing sound in Python is a useful feature in various applications, from games and user notifications to more complex audio-processing projects. Depending on the format (WAV, MP3, etc.) and the functionality required, you can select the most suitable library for your project. Let’s dive into the different methods.

Method 1: Playing Sound with the playsound Module

Run the following command to install the packages:

pip install playsound
  • The playsound module contains only a single function named playsound().
  • It requires one argument: the path to the file with the sound we have to play. It can be a local file, or a URL.
  • There’s an optional second argument, block, which is set to True by default. We can set it to False for making the function run asynchronously.
  • It works with both WAV and MP3 files.
  • While using playsound module to play sounds giving relative path as input, you may get error message "The specified device is not open or is not recognized by MCI." A workaround is to use an older version of playsound module. Following commands should be run: pip uninstall playsound. pip install playsound==1.2.2

Example: For WAV format

Python
# import required module
from playsound import playsound

# for playing note.wav file
playsound('/path/note.wav')
print('playing sound using  playsound')

Output:

Example: For mp3 format

Python
# import required module
from playsound import playsound

# for playing note.mp3 file
playsound('/path/note.mp3')
print('playing sound using  playsound')

Output:

Method 2: Using the pydub Module for Advanced Audio Processing

Run the following commands to install the packages:

sudo apt-get install ffmpeg libavcodec-extra
pip install pydub

Note: You can open WAV files with python. For opening mp3, you'll need ffmpeg or libav.

This module uses the from_wav() method for playing wav file and from_mp3() method for playing an mp3 file.  The play() method is used to play the wav and mp3 file:

Example 1: For WAV format

Python
# import required modules
from pydub import AudioSegment
from pydub.playback import play

# for playing wav file
song = AudioSegment.from_wav("note.wav")
print('playing sound using  pydub')
play(song)

Output:

Example 2: For mp3 format

Python
# import required module
from pydub import AudioSegment
from pydub.playback import play

# for playing mp3 file
song = AudioSegment.from_mp3("note.mp3")
print('playing sound using  pydub')
play(song)

Output:

Method 3: Playing Sound with the tksnack Module

The tksnack module depends upon a module named tkinter to activate a tk object in the python script. You must install tkinter and tksnack packages for Python. Run the following commands to install the packages:

sudo apt-get install python3-tk
sudo apt-get install python3-tksnack

The play() method is used to play the audio files. The blocking argument states that the sound will play asynchronously.

Example: 

Python
# import required modules
from Tkinter import *
import tkSnack

# initialize tk object to use tksnack
root = Tk()
tkSnack.initializeSnack(root)

# play sound
snd = tkSnack.Sound()
snd.read('note.wav')
print('playing sound using tkSnack')
snd.play(blocking=1)

Output:

Method 4: Playing Sound Using a Native Player

In this method, we play sounds natively on our system. This method plays the audio file with an external player installed on your terminal.

Example 1: For Mac OS X

Python
# import required module
import os

# play sound
file = "note.wav"
print('playing sound using native player')
os.system("afplay " + file)

Output:

Example 2: For Linux

Python
# import required module
import os

# play sound
file = "note.mp3"
print('playing sound using native player')
os.system("mpg123 " + file)

Output:

Method 5: Using the simpleaudio Module

This is mainly designed to play WAV files and NumPy arrays. Run the following command to install the packages:

$ sudo apt-get install libasound2-dev
$ pip3 install simpleaudio

The play() method is used to play the audio files.

Example:

Python
# import required module
import simpleaudio as sa

# define an object to play
wave_object = sa.WaveObject.from_wave_file('note.wav')
print('playing sound using simpleaudio')

# define an object to control the play
play_object = wave_object.play()
play_object.wait_done()

Output:

Conclusion

In this guide, we covered multiple ways to play sound in Python using various libraries like playsound, pydub, tksnack, simpleaudio, and native players. Each method has its strengths and weaknesses, so the best option depends on your specific use case.

For basic sound playback, playsound is quick and simple. If you need more advanced functionality, such as audio processing, consider using pydub. For GUI-based applications, tksnack could be a good fit. And if you're looking for low-latency WAV playback, simpleaudio is the way to go.

Whether you're building a game, a notification system, or a more complex audio processing tool, these libraries will help you get sound playing in Python with ease


Article Tags :
Practice Tags :

Similar Reads