Open In App

How to add Music Playlist in Pygame?

Improve
Improve
Like Article
Like
Save
Share
Report

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 – 

  • pygame.mixer.music.load(): This will load the music file/filename object.
  • pygame.mixer.music.play(loops, 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.
  • pygame.mixer.music.queue(): 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.
  • pygame.mixer.music.rewind(): This rewinds the playback of the current music file to the beginning.
  • pygame.mixer.music.pause(): This pauses the playback of the current music file.
  • pygame.mixer.music.set_endevent(): This sends a signal i.e, an event. If the music file has finished playing, an event will be queued. 
  • pygame.mixer.music.get_busy(): 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 pygame.mixer.music.load() and simultaneously deleting the first entry from the list.
  • On loading the first song, we’ll play it using pygame.mixer.music.play().
  • Subsequently, we will also queue the second entry by using pygame.mixer.music.queue() and delete the same from our list.
  • Whenever music ends, we’ll be hosting an event pygame.MUSIC_END  by using pygame.mixer.music.set_endevent() 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 pygame.mixer.music.get_busy() returns false and we break the for-loop and while-loop bringing our program to the end.

Below is the implementation:

Python




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:



Last Updated : 18 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads