Python is well known for its powerful libraries and frameworks, that helps developers a lot when they are just getting started on any Python project. Python is also capable of handling different types of file formats, including, text, database, videos and audio, for cross-platform (Windows, Mac, and Linux). And in this article, we will learn how can we play a song or sound using Python and its different python libraries .
We will explore some of the Python libraries and implement Python code to play sounds. To get full out of this tutorial, you must have the knowledge of basic Python, including, what are libraries and how to install third-party libraries in Python using
command. Even if you do not know, what is a library, do not worry, let's define it first.
What is a Library in Python?
A library is a collection of pre-written code, and a Python library is nothing but just a pre-written Python code. We include it in our main file, using the
keywords. We import it into our program so we can leverage the functionality of the library code and do not reinvent the code that is already written. Sometimes we also use the term module for a library, and in the programming world module and library are two interchangeable terms, so don't be confused when we say module instead of the library, they both means the same. Now let's discuss the different modules or libraries we can use to play sound in Python.
1. Play Sound in Python using playsound module
Playsound is a third-party Python module, and as the name suggests this module is used to handle audio data formats such as mp3, and wav. playsound is a single file python module with no dependency which make it a lightweight module. Still, as a third-party module, you need to install it for your Python environment before using it. To install play sound you can write the following pip install command on your terminal or command prompt.
pip install playsound
Python program to play sound using playsound module
# import the module from playsound import playsound # play the song print("Song is playing......") my_song = 'song.mp3' playsound(my_song)
Song is playing......
When you execute the program the specified song will start playing in the background. To stop the program you can hit
The audio or sound file must be in the smae directory of your Python file. If both are in not in the same directory, then you need to specify the full path. such as:
2. Play Sound in Python using os module
You can also play the music on your system music application. For that, you can use the Python inbuilt
module is generally used to handle operating system tasks using Python, but we can also use it to play the music in the music player.
is the Python inbuilt module so we do not need to install it separately.
Python program to play sound using os module
# import the module import os my_song = "song.mp3" # play song on the music player print("Song is playing....") os.system(my_song)
Song is playing....
In this Python article, we discussed the two different approaches to play sounds using Python modules. If you want to play the sound inside a python application then you should go with
library, but if you want to play the song or audio on the user music player then you should go with he
module. To know how to record and play the recorded audio in Python
People are also reading:
Leave a Comment on this Post