Play audio with Python


How can I play audio (it would be like a 1 second sound) from a Python script?

It would be best if it was platform independent, but firstly it needs to work on a Mac.

I know I could just execute the afplay file.mp3 command from within Python, but is it possible to do it in raw Python? I would also be better if it didn’t rely on external libraries.

Asked By: Josh Hunt



You can find information about Python audio here:

It doesn’t look like it can play .mp3 files without external libraries. You could either convert your .mp3 file to a .wav or other format, or use a library like PyMedia.

Answered By: Paige Ruten

Your best bet is probably to use pygame/SDL. It’s an external library, but it has great support across platforms.


You can find more specific documentation about the audio mixer support in the documentation

Answered By: TML

You can see this:

s = Sound()'sound.wav')
Answered By: user1926182

In pydub we’ve recently opted to use ffplay (via subprocess) from the ffmpeg suite of tools, which internally uses SDL.

It works for our purposes – mainly just making it easier to test the results of pydub code in interactive mode – but it has it’s downsides, like causing a new program to appear in the dock on mac.

I’ve linked the implementation above, but a simplified version follows:

import subprocess

def play(audio_file_path):["ffplay", "-nodisp", "-autoexit", audio_file_path])

The -nodisp flag stops ffplay from showing a new window, and the -autoexit flag causes ffplay to exit and return a status code when the audio file is done playing.

edit: pydub now uses pyaudio for playback when it’s installed and falls back to ffplay to avoid the downsides I mentioned. The link above shows that implementation as well.

Answered By: Jiaaro

If you’re on OSX, you can use the “os” module or “subprocess” etc. to call the OSX “play” command. From the OSX shell, it looks like

play “bah.wav”

It starts to play in about a half-second on my machine.

Answered By: Moondoggy

Also on OSX – from SO, using OSX’s afplay command:

import subprocess["afplay", "path/to/audio/file"])

UPDATE: All this does is specify how to do what the OP wanted to avoid doing in the first place. I guess I posted this here because what OP wanted to avoid was the info I was looking for. Whoops.

Answered By: MikeiLL

Sorry for the late reply, but I think this is a good place to advertise my library …

AFAIK, the standard library has only one module for playing audio: ossaudiodev.
Sadly, this only works on Linux and FreeBSD.

UPDATE: There is also winsound, but obviously this is also platform-specific.

For something more platform-independent, you’ll need to use an external library.

My recommendation is the sounddevice module (but beware, I’m the author).

The package includes the pre-compiled PortAudio library for Mac OS X and Windows, and can be easily installed with:

pip install sounddevice --user

It can play back sound from NumPy arrays, but it can also use plain Python buffers (if NumPy is not available).

To play back a NumPy array, that’s all you need (assuming that the audio data has a sampling frequency of 44100 Hz):

import sounddevice as sd, 44100)

For more details, have a look at the documentation.

It cannot read/write sound files, you’ll need a separate library for that.

Answered By: Matthias

It is possible to play audio in OS X without any 3rd party libraries using an analogue of the following code. The raw audio data can be input with wave_wave.writeframes. This code extracts 4 seconds of audio from the input file.

import wave
import io
from AppKit import NSSound

wave_output = io.BytesIO()
wave_shell =, mode="wb")
file_path = 'SINE.WAV'
input_audio =
input_audio_frames = input_audio.readframes(input_audio.getnframes())


seconds_multiplier = input_audio.getnchannels() * input_audio.getsampwidth() * input_audio.getframerate()


wave_data =
audio_stream = NSSound.alloc()
Answered By: Aaron

Aaron’s answer appears to be about 10x more complicated than necessary. Just do this if you only need an answer that works on OS X:

from AppKit import NSSound

sound = NSSound.alloc()
sound.initWithContentsOfFile_byReference_('/path/to/file.wav', True)

One thing… this returns immediately. So you might want to also do this, if you want the call to block until the sound finishes playing.

from time import sleep


Edit: I took this function and combined it with variants for Windows and Linux. The result is a pure python, cross platform module with no dependencies called playsound. I’ve uploaded it to pypi.

pip install playsound

Then run it like this:

from playsound import playsound
playsound('/path/to/file.wav', block = False)

MP3 files also work on OS X. WAV should work on all platforms. I don’t know what other combinations of platform/file format do or don’t work – I haven’t tried them yet.

Answered By: ArtOfWarfare

Simply You can do it with the help of cvlc-
I did it in this way:

import os
os.popen2("cvlc /home/maulo/selfProject/task.mp3 --play-and-exit")

/home/maulo/selfProject/task.mp3. This is the location of my mp3 file.
with the help of “–play-and-exit” you will be able to play again the sound without ending the vlc process.

Answered By: pyAddict

Take a look at Simpleaudio, which is a relatively recent and lightweight library for this purpose:

> pip install simpleaudio


import simpleaudio as sa

wave_obj = sa.WaveObject.from_wave_file("path/to/file.wav")
play_obj =

Make sure to use uncompressed 16 bit PCM files.

Answered By: Erwin Mayer

Try PySoundCard which uses PortAudio for playback which is available on many platforms.
In addition, it recognizes “professional” sound devices with lots of channels.

Here a small example from the Readme:

from pysoundcard import Stream

"""Loop back five seconds of audio data."""

fs = 44100
blocksize = 16
s = Stream(samplerate=fs, blocksize=blocksize)
for n in range(int(fs*5/blocksize)):
Answered By: Stefan Balke
Put this at the top of your python script you are writing:

import subprocess

If the wav file IS in the directory of the python script:

f = './mySound.wav'

If the wav file IS NOT in the directory of the python script:

f = 'mySound.wav'
subprocess.Popen(['aplay','-q', 'wav/' + f)

If you want to learn more about aplay:

man aplay
Answered By: Crawsome

This is the easiest & best iv’e found. It supports Linux/pulseaudio, Mac/coreaudio, and Windows/WASAPI.

import soundfile as sf
import soundcard as sc

default_speaker = sc.default_speaker()
samples, samplerate ='bell.wav'), samplerate=samplerate)

See and for tons of other super-useful features.

Answered By: n00p

Try playsound which is a Pure Python, cross platform, single function module with no dependencies for playing sounds.

Install via pip:

$ pip install playsound

Once you’ve installed, you can use it like this:

from playsound import playsound
Answered By: yehan jaya

Pypi has a list of modules for python in music. My favorite would be jython because it has more resources and libraries for music. As example of of code to play a single note from the textbook:

# Demonstrates how to play a single note.

from music import *   # import music library
note = Note(C4, HN)   # create a middle C half note 
Play.midi(note)       # and play it!
Answered By: Kardi Teknomo

To play a notification sound using python, call a music player, such as vlc. VLC prompted me to use its commandline version, cvlc, instead.

from subprocess import call
call(["cvlc", "--play-and-exit", "myNotificationTone.mp3"])

It requires vlc to be preinstalled on the device. Tested on Linux(Ubuntu 16.04 LTS); Running Python 3.5.

Answered By: amarVashishth

Mac OS I tried a lot of codes but just this works on me

import pygame
import time
pygame.init()'fire alarm sound.mp3') *On my project folder*
i = 0
while i<10:, start=0.0)
    time.sleep(10)*to protect from closing*
    i = i + 1
Answered By: Captain Django

Install playsound package using :

pip install playsound


from playsound import playsound
playsound("file locationaudio.p3")
Answered By: Harish

Try sounddevice

If you don’t have the module enter
pip install sounddevice in your terminal.

Then in your preferred Python script (I use Juypter), enter

import sounddevice as sd, sr) will play what you want through Python

The best way to get the audio and samplerate you want is with the librosa module. Enter this in terminal if you don’t have the librosa module.

pip install librosa

audio, sr = librosa.load('wave_file.wav')

Whatever wav file you want to play, just make sure it’s in the same directory as your Python script. This should allow you to play your desired wav file through Python



Once audio is a “librosa” data object, Python sees it as a numpy array. As an experiment, try playing a long (try 20,000 data points) thing of a random numpy array. Python should play it as white noise. The sounddevice module plays numpy arrays and lists as well.

Answered By: Charlie Carrera

In a Colab notebook you can do:

from IPython.display import Audio
Audio(waveform, Rate=16000)
Answered By: Axel Bregnsbo

I recently made my Music Player support all audio files locally. I did this by figuring out a way to use the vlc python module and also the VLC dll files.
You can check it out:

Answered By: Elijah

It’s Simple. I did it this way.

For a wav file

from IPython.display import Audio
from import read

fs, data = read('StarWars60.wav', mmap=True)  # fs - sampling frequency
data = data.reshape(-1, 1)
Audio(data = data[:, 0], rate = fs)

For mp3 file

import IPython.display import Audio

Answered By: Aastha Varma

For those who use Linux and the other packages haven’t worked on MP3 files, audioplayer worked fine for me:

from audioplayer import AudioPlayer
Answered By: gleitonfranco

This library aims to be simple, cross-platform and have many features:

It requires a libwinmedia shared library, which you can download in Releases tab.

You can install it using pip install libwinmedia


import libwinmedia

player = libwinmedia.Player(True)

player.set_position_callback(lambda position: print(f"{position} ms."))
media = libwinmedia.Media("test.mp3")
Answered By: mytja

This should work on Linux, Mac or Windows:

from preferredsoundplayer import *

Should work for mp3 also.

In Linux it will try up to 4 different methods. In Windows it uses winmm.dll. In Mac it uses afplay.

I wrote it because:

  • I kept having issues with cross-compatibility for playing sounds.
  • It also manually garbage collects calls to the winmm.dll player in Windows and appropriate closes finished sounds.
  • It has no dependencies, other than what comes with Windows 10, the standard Linux kernel, MacOS 10.5 or later, and the Python Standard Library.

You can install using pip install preferredsoundplayer (see project) or just utilize the source code which is a single file (source code) .

Answered By: garydavenport73
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.