playsound
00:00
playsound
aims to be a “pure Python, cross-platform, single function module with no dependencies for playing sounds.” In other words, if all you need to do is play a sound file, this will get it done. In fact, you can play a sound with a single line of code.
00:17 To get some housekeeping out of the way, we’re going to be working with a number of new libraries, so it’s probably a good idea to create an environment to work in.
00:26
I’m going to use pipenv
, but whatever process works for you is okay. I’ve also gone ahead and made two audio files, hello.mp3
and hello.wav
, which are an MP3 and a WAV file.
00:42
The content of these files isn’t important, but I’m going to use them to show the compatibilities between different file types with the libraries that we talk about. From the terminal, let’s go ahead and install playsound
.
01:01
All right. That’s all installed. Inside your text editor, you can then just go ahead and say from playsound import playsound
. And to play that sound, just call playsound()
and pass in the filename.
01:15 Let’s go ahead with the MP3 first. I’m going to save this.
01:29
“Hey, this is an MP3.” And it worked! If you’re on Mac and you see an error about a missing AppKit
package, then just go ahead and make sure you also install this PyObjC
.
01:56
What this will do is form a bridge between Python and Objective-C on your Mac. All right! So you’ve got playsound
working. In the next video, you’re going to see how to use simpleaudio
for playing WAV files.
Joe Tatusko RP Team on Feb. 27, 2020
Hi lemmehisss!
Does playsound work with any other audio files that you have? From looking around it might have an issue handling certain filenames and audio bitrates.
Could you let me know what the full path and bitrate of punch.mp3 is? Thanks!
Become a Member to join the conversation.
lemmehisss on Feb. 27, 2020
Hi, I get the below error .Please help.
I used the above simple code and also ” pip install playsound” done.
Traceback (most recent call last): File “psound.py”, line 3, in <module> playsound (‘punch.mp3’) File “C:\Users\am8615\AppData\Local\Continuum\anaconda3\lib\site-packages\playsound.py”, line 35, in _playsoundWin winCommand(‘open “’ + sound + ‘” alias’, alias) File “C:\Users\am8615\AppData\Local\Continuum\anaconda3\lib\site-packages\playsound.py”, line 31, in winCommand raise PlaysoundException(exceptionMessage) playsound.PlaysoundException: Error 277 for command: open “punch.mp3” alias playsound_0.4895153817297281 A problem occurred in initializing MCI.