Get Started

Play an audio file with PyDub

If you're working with audio files, chances are you want to listen to them.

PyDub's playback module provides a function called play() which can be passed an AudioSegment. Running the play() function with an AudioSegment passed in will play the AudioSegment out loud.

This can be helpful to check the quality of your audio files and assess any changes you need to make.

In this exercise you'll see how simple it is to use the play() function.

Remember: to use the play() function, you'll need simpleaudio or pyaudio installed for .wav files and ffmpeg for other kinds of files.

This is a part of the course

“Spoken Language Processing in Python”

View Course

Exercise instructions

  • Import play from the pydub.playback module.
  • Call play() whilst passing it the wav_file AudioSegment.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import AudioSegment and play
from pydub import AudioSegment
from pydub.playback import ____

# Create an AudioSegment instance
wav_file = AudioSegment.from_file(file="wav_file.wav", 
                                  format="wav")

# Play the audio file
____(____)

This exercise is part of the course

Spoken Language Processing in Python

AdvancedSkill Level
4.7+
3 reviews

Learn how to load, transform, and transcribe speech from raw audio files in Python.

Not all audio files come in the same shape, size or format. Luckily, the PyDub library by James Robert provides tools which you can use to programmatically alter and change different audio file attributes such as frame rate, number of channels, file format and more. In this chapter, you'll learn how to use this helpful library to ensure all of your audio files are in the right shape for transcription.

Exercise 1: Introduction to PyDubExercise 2: Import an audio file with PyDubExercise 3: Play an audio file with PyDub
Exercise 4: Audio parameters with PyDubExercise 5: Adjusting audio parametersExercise 6: Manipulating audio files with PyDubExercise 7: Turning it down... then upExercise 8: Normalizing an audio file with PyDubExercise 9: Chopping and changing audio filesExercise 10: Splitting stereo audio to mono with PyDubExercise 11: Converting and saving audio files with PyDubExercise 12: Exporting and reformatting audio filesExercise 13: Manipulating multiple audio files with PyDubExercise 14: An audio processing workflow

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free