Last modified: Apr 16, 2026 By Alexander Williams
Play Audio in Python: Simple Methods & Libraries
Python is a powerful language for many tasks. One common task is playing audio files. This can be for games, applications, or automation scripts.
This guide will show you how to play sound in Python. We will cover simple methods and useful libraries. You will learn with clear, working code examples.
Why Play Audio with Python?
Playing audio programmatically is useful. You might build a music player. You could add sound effects to a game. Or you might create an alert system.
Python makes this easy. It has several libraries for handling audio. We will explore the most popular and beginner-friendly options.
Prerequisites for Playing Audio
Before you start, ensure you have Python installed. You will also need to install some external libraries. We use a package manager called pip for this.
Open your terminal or command prompt. You can check your Python version with a simple command.
# Check Python version
python --version
Now, let's look at the different ways to play audio.
Method 1: Using the Playsound Library
The playsound library is the simplest. It is a pure Python, cross-platform module. It requires only one function call to play a sound.
First, you need to install it. Use pip in your command line.
pip install playsound
Once installed, you can use it in your code. The main function is playsound(). You just need to provide the path to your audio file.
# Import the playsound function
from playsound import playsound
# Play an audio file
playsound('path/to/your/audio.mp3')
print("Audio playback finished.")
This code will play the MP3 file. The program will wait until the sound finishes. It supports MP3 and WAV files well.
The advantage is its simplicity. The disadvantage is limited control. You cannot pause, stop, or get playback status easily.
Method 2: Using Pygame for More Control
Pygame is a popular library for making games. It has robust features for handling audio. It gives you more control over playback.
Install pygame using pip.
pip install pygame
Pygame requires you to initialize its mixer module. This module handles sound loading and playback. Here is a basic example.
import pygame
import time
# Initialize the pygame mixer
pygame.mixer.init()
# Load the sound file
sound = pygame.mixer.Sound('path/to/your/audio.wav')
# Play the sound
sound.play()
# Let the sound play for its duration (5 seconds in this wait)
time.sleep(5)
print("Playback complete.")
With pygame, you can pause, stop, and set volume. You can also play sounds in loops. This is great for background music or sound effects.
For example, to stop playback, you can use sound.stop().
Method 3: Using Pydub with SimpleAudio
Pydub is a high-level audio library. It works well with simpleaudio or pyaudio for playback. This combo is powerful for audio manipulation.
First, install the required libraries.
pip install pydub simpleaudio
Pydub can handle many audio formats. It uses simpleaudio for fast, cross-platform playback. Here is how you use it.
from pydub import AudioSegment
from pydub.playback import play
# Load an audio file (supports mp3, wav, ogg, etc.)
audio = AudioSegment.from_file("path/to/your/sound.mp3", format="mp3")
# Play the audio
play(audio)
print("Audio played successfully.")
Pydub is excellent if you need to edit audio. You can change volume, trim, or merge files before playing. The playback is non-blocking with simpleaudio backend.
Choosing the Right Library
How do you choose which library to use? It depends on your project's needs.
Use playsound for the simplest case. You just need to play a file once without any controls.
Use pygame if you need control. This is ideal for games or applications with interactive sound.
Use pydub if you need to manipulate audio. It's perfect for applications that edit or process sound before playing.
For more on handling different file types, see our guide on Python file handling.
Common Issues and Troubleshooting
You might encounter some problems. Here are common issues and their fixes.
File Not Found Error: Ensure the file path is correct. Use absolute paths if needed.
Codec Not Supported: Some libraries need extra codecs. For MP3 with pydub, you might need ffmpeg. Install it via your system package manager.
No Sound Output: Check your system volume. Ensure your audio device is working. Verify the library installed correctly.
Advanced Example: Playing a List of Sounds
Let's create a more advanced script. This will play a list of audio files one after another. We will use the playsound library for simplicity.
from playsound import playsound
import os
# List of audio file paths
audio_files = [
'sound1.mp3',
'sound2.wav',
'sound3.mp3'
]
# Loop through and play each file
for audio_file in audio_files:
# Check if the file exists before playing
if os.path.exists(audio_file):
print(f"Now playing: {audio_file}")
playsound(audio_file)
else:
print(f"File not found: {audio_file}")
print("Playlist finished.")
Now playing: sound1.mp3
Now playing: sound2.wav
Now playing: sound3.mp3
Playlist finished.
Conclusion
Playing audio in Python is straightforward. You have several great libraries to choose from.
For quick playback, use playsound. For game development, use pygame. For audio editing and playback, use pydub.
Start with a simple script. Experiment with different file formats and controls. Sound can make your Python projects much more engaging and interactive.
Remember to handle file paths carefully. Install the necessary backend codecs when required. Now you have the tools to add sound to your next Python project.