DEV Community

Susmita Dey
Susmita Dey

Posted on • Edited on

Play Song In Python Using PlaySound Module

Hello everyone!! Today we will discuss on how to play song or music using playsound module in python.

Prerequisites:-

  1. Download the latest version of Python from the official website python.org by just clicking on the download option and install it in your computer as you install other applications.
  2. Code Editor like Visual Studio Code or others of your choice where you can compile and run your python code.
  3. If you're using Visual Studio Code, then install this extension below. Python Extension

Steps to Be Followed:-

  1. You have to install the playsound module in our computer by running the command pip install playsound.
    You can use Command Prompt(CMD) or Windows Powershell as per your choice and just type or copy the command.
    If you're using Visual Studio Code then you can run it here in the terminal itself and then press enter. It gets install very soon.

  2. Create a file here in your current folder with ".py" extension.

  3. Now type or copy the following two lines of code and paste it in your IDE. You can do the same thing from the playsound module documentation and just remove the items from the playsound module and give the correct path of the song or music you wanna play or you can type a

    name of a “.wav” file or “.mp3” file if you have it in the same(current) folder.

Note:- You have to do the same like removing the items from the playsound module give your own path if you're coping the code below.


Code to Run:-

from playsound import playsound

print("Playing Song using playsound")
playsound("C:\\Users\\user\\Music\\Chal Ghar Chalen - Malang 128 Kbps.mp3")

Enter fullscreen mode Exit fullscreen mode

⚠️ Warning

One most important thing here is that you must put double slash as there's only a single slash here because in 99%
cases it gives an error but in 1% cases it runs without error like this. So, it's always better to use double slashes when you’re giving path of a file.

Now save your file and run it in the terminal and you'll hear the sound. I have used here a Bollywood song "Chal Ghar Chalen by Arijit Singh and Mithoon" from the movie Malaang.


TIPS

You can also search in the search box of your computer “mp3” and if you have such files and just right-click on it and then click on copy full path option and paste it in the playsound function.

So, This was just the code of two lines; very simple and easy.


VIDEO

Hope this helps you. Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my YouTube Channel and connect on LinkedIn or Twitter.
Also, feel free to support my work.😊

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.