Skip to content
Related Articles

Related Articles

Improve Article

How to add Music Playlist in Pygame?

  • Last Updated : 18 Jul, 2021
Geek Week

In this article, we are going to see how to add a music playlist in Pygame.

Pygame has a method, pygame.mixer() which has all the functionalities regarding audio operations like playing a song, queuing a song, setting the volume, rewind, stop, pause, resume, and many more. Functions provided by this class are – 

  • This will load the music file/filename object.
  •, start, fade_ms ): Starts the playback of the music which has been loaded. This takes 3 arguments
    • loops: It is an optional integer argument that states how many times the song should repeat. If set to -1, then it will loop indefinitely.
    • start: It is an optional float argument, which denotes the position in time and from where the music should start playing.
    • fade_ms: It is an optional integer argument. It makes the music start from 0 volume and fades up to full volume at the given time.
  • This will queue a song at the end of the current music file. As soon as the current file ends, the next queued music file gets played. You can also queue multiple songs and this will be our main logic for building a playlist.
  • This rewinds the playback of the current music file to the beginning.
  • This pauses the playback of the current music file.
  • This sends a signal i.e, an event. If the music file has finished playing, an event will be queued. 
  • This returns a boolean value, indicating if a music file has currently been played or not. 

Adding Playlist in Pygame

Inorder to create a playlist in Pygame we’ll be using all the above-mentioned methods to form a flow in which all our chosen music files will be pushed to list and each one of those files will be queued and played one after the other in sequential order. This logic can be implemented as follows:

  • Load the first entry of the list containing music files into pygame by using and simultaneously deleting the first entry from the list.
  • On loading the first song, we’ll play it using
  • Subsequently, we will also queue the second entry by using and delete the same from our list.
  • Whenever music ends, we’ll be hosting an event pygame.MUSIC_END  by using to notify the player to queue the next song from the list.
  • Once all the music files from our playList has been played/queued then returns false and we break the for-loop and while-loop bringing our program to the end.

Below is the implementation:


import pygame
# setting up pygame
def insert_into_playlist(playlist, music_file):
    # Adding songs file in our playlist
def start_playlist(playList):
    # Loading first audio file into our player[0])
    # Removing the loaded song from our playlist list
    # Playing our music
    # Queueing next song into our player[0])
    # setting up an end event which host an event
    # after the end of every song
    # Playing the songs in the background
    running = True
    while running:
        # checking if any event has been
        # hosted at time of playing
        for event in pygame.event.get():
            # A event will be hosted
            # after the end of every song
            if event.type == pygame.MUSIC_END:
                print('Song Finished')
                # Checking our playList
                # that if any song exist or
                # it is empty
                if len(playList) > 0:
                    # if song available then load it in player
                    # and remove from the player
            # Checking whether the 
            # player is still playing any song
            # if yes it will return true and false otherwise
            if not
                print("Playlist completed")
                # When the playlist has
                # completed playing successfully
                # we'll go out of the
                # while-loop by using break
                running = False
if __name__ == '__main__':
    # This list is going to be
    # our playlist as we can
    # only queue one song at a
    # time by using `.queue()` method
    # therefore we are using list
    # and will queue song one by one.
    playList = []
    insert_into_playlist(playList, 'eg1.wav')
    insert_into_playlist(playList, 'eg2.wav')
    insert_into_playlist(playList, 'eg3.wav')


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :