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 –
- 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:
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