Open In App

How to add Music Playlist in Pygame?

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 – 



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:

Below is the implementation:






import pygame
  
# setting up pygame
pygame.init()
  
  
def insert_into_playlist(playlist, music_file):
    
    # Adding songs file in our playlist
    playlist.append(music_file)
  
  
def start_playlist(playList):
    
    # Loading first audio file into our player
    pygame.mixer.music.load(playList[0])
      
    # Removing the loaded song from our playlist list
    playList.pop(0)
  
    # Playing our music
    pygame.mixer.music.play()
  
    # Queueing next song into our player
    pygame.mixer.music.queue(playList[0])
    playList.pop(0)
  
    # setting up an end event which host an event
    # after the end of every song
    pygame.mixer.music.set_endevent(pygame.MUSIC_END)
  
    # 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
                    pygame.mixer.music.queue(playList[0])
                    playList.pop(0)
  
            # Checking whether the 
            # player is still playing any song
            # if yes it will return true and false otherwise
            if not pygame.mixer.music.get_busy():
                print("Playlist completed")
                  
                # When the playlist has
                # completed playing successfully
                # we'll go out of the
                # while-loop by using break
                running = False
                break
  
  
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')
  
    start_playlist(playList)

Output:


Article Tags :