How to Play sounds in Python

Posted in /  

How to Play sounds in Python
vinaykhatri

Vinay Khatri
Last updated on December 22, 2024

    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 pip install 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 from and import 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)

    Output

    Song is playing......

    When you execute the program the specified song will start playing in the background. To stop the program you can hit CTRL+C .

    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: my_song =r'C:\Users\tsmehra\Desktop\code\song.mp3'

    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 os module. The os 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. os 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)

    Output

    Song is playing....

    Wrapping Up!

    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 playsound library, but if you want to play the song or audio on the user music player then you should go with he os module. To know how to record and play the recorded audio in Python click here .

    People are also reading:

    Leave a Comment on this Post

    0 Comments