Play Sound in Python

Playing sound in Python can enhance applications with audio feedback, music, or sound effects. Python offers several libraries for audio playback, from simple solutions like playsound to more advanced options like pygame and pyglet.

Using the playsound Library

The playsound library provides the simplest way to play audio files with minimal setup. Install it using pip install playsound.

Example

Here's how to play a sound file using playsound ?

# Note: This requires an actual audio file to work
from playsound import playsound

# Provide the path to your sound file
sound_file = "example.mp3"

# Play the sound file
playsound(sound_file)
print("Sound played successfully!")

The playsound function supports various audio formats including MP3, WAV, and blocks execution until playback completes.

Advanced Audio with pygame

The pygame library offers more control over audio playback, including volume control, simultaneous sounds, and sound effects. Install it using pip install pygame.

Example

Here's how to use pygame for audio playback ?

# Note: This requires pygame installation and an audio file
import pygame
import time

# Initialize the pygame mixer
pygame.mixer.init()

# Load a sound file (requires actual file)
sound_file = "example.wav"
sound = pygame.mixer.Sound(sound_file)

# Play the sound
sound.play()

# Wait for the sound to finish playing
time.sleep(sound.get_length())

print("Pygame sound playback completed!")

Pygame allows multiple simultaneous sounds, volume control with sound.set_volume(), and sound effects like looping.

Professional Audio with pyglet

The pyglet library provides advanced features including positional audio, pitch shifting, and custom audio streaming. Install it using pip install pyglet.

Example

Here's a basic example using pyglet ?

# Note: This requires pyglet installation and an audio file
import pyglet

# Create a pyglet player object
player = pyglet.media.Player()

# Load a sound file (requires actual file)
sound_file = "example.wav"
source = pyglet.media.load(sound_file, streaming=False)

# Queue the source to the player
player.queue(source)

# Play the sound
player.play()

# Keep the application running
print("Playing sound with pyglet...")

# Note: In a real application, you'd use pyglet.app.run()
# or handle the event loop appropriately

Pyglet excels at 3D positional audio, real?time audio streaming, and complex audio effects for game development.

Comparison

Library Complexity Best For Key Features
playsound Simple Basic audio playback One function call
pygame Moderate Game development Multiple sounds, volume control
pyglet Advanced Professional applications 3D audio, streaming, effects

Conclusion

Choose playsound for simple audio playback, pygame for game development with multiple sounds, and pyglet for professional applications requiring advanced audio features. Each library serves different complexity levels and use cases in Python audio programming.

Updated on: 2026-03-27T09:44:36+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements