In this article we will walk through process of Create Music Player in Python and Pygame module that can play only mp3 songs. Pygame is popular Python module used for building games and multimedia applications. It provides a set of tools and features that make it easy to develop games and multimedia applications in Python. In this tutorial we will leverage these features to create a simple music player that can play mp3 files.
Step 1: Importing Pygame and OS modules
The first step is import required modules – pygame and os. Pygame module will be used for playing music while os module is used to access the music directory and files.
Python Program to Convert Speech to Text with Output
Real Time Currency Converter in Python with Output
Python Program to Convert Text to Speech
import pygame
import os
Step 2: Initializing Pygame mixer
After importing module we initialize Pygame mixer module which provides set of functions to load, play and control music files.
pygame.init()
pygame.mixer.init()
Step 3: Setting up Pygame display module
To display music player window we set up Pygame display module and also set caption the window.
display = pygame.display.set_mode((600, 400))
pygame.display.set_caption('MP3 Music Player')
Step 4: Setting up font
We set font for text that will be displayed music player window.
font = pygame.font.SysFont('Arial', 25)
Step 5: Accessing music directory
We set path to music directory and use os module to get list of all the mp3 file in directory.
music_dir = 'path/to/music/folder'
songs = [song for song in os.listdir(music_dir) if song.endswith('.mp3')]
Step 6: Loading and playing first song
We set current_song variable to keep track of current song being played. Then we load and play first song in list.
current_song = 0
pygame.mixer.music.load(os.path.join(music_dir, songs[current_song]))
pygame.mixer.music.play()
Step 7: Setting up main loop
We set main loop that will handle events such as:
key presses
mouse clicks
We first get events using Pygame event module.
while True:
events = pygame.event.get()
Step 8: Handling quit event
We check for quit event and exit program if user closes window.
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
exit()
Step 9: Handling key events
We check for key events, such as left and right arrow key presses play the previous or next song accordingly.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_song = (current_song - 1) % len(songs)
pygame.mixer.music.load(os.path.join(music_dir, songs[current_song]))
pygame.mixer.music.play()
elif event.key == pygame.K_RIGHT:
current_song = (current_song + 1) % len(songs)
pygame.mixer.music.load(os.path.join(music_dir, songs[current_song]))
pygame.mixer.music.play()
Step 10: Displaying current song name
We fill screen with black color and draw name of current song on screen using font we set up earlier.
display.fill((0, 0, 0))
text = font.render(songs[current_song], True, (255, 255, 255))
display.blit(text, (10,
Complete code implementation
Here is complete code implementation for Music Player using Python:
import pygame
import os
pygame.init()
# set up the Pygame mixer module
pygame.mixer.init()
# set up the Pygame display module
display = pygame.display.set_mode((600, 400))
pygame.display.set_caption('MP3 Music Player')
# set up the font
font = pygame.font.SysFont('Arial', 25)
# set up the path to the music directory
music_dir = 'path/to/music/folder'
# get a list of all the mp3 files in the music directory
songs = [song for song in os.listdir(music_dir) if song.endswith('.mp3')]
# set up the current song index
current_song = 0
# load the first song in the list
pygame.mixer.music.load(os.path.join(music_dir, songs[current_song]))
# play the loaded song
pygame.mixer.music.play()
# set up the loop
while True:
# get the events
events = pygame.event.get()
for event in events:
# check for the quit event
if event.type == pygame.QUIT:
pygame.quit()
exit()
# check for key events
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
# play the previous song
current_song = (current_song - 1) % len(songs)
pygame.mixer.music.load(os.path.join(music_dir, songs[current_song]))
pygame.mixer.music.play()
elif event.key == pygame.K_RIGHT:
# play the next song
current_song = (current_song + 1) % len(songs)
pygame.mixer.music.load(os.path.join(music_dir, songs[current_song]))
pygame.mixer.music.play()
# fill the screen with black color
display.fill((0, 0, 0))
# draw the current song name on the screen
text = font.render(songs[current_song], True, (255, 255, 255))
display.blit(text, (10, 10))
# update the display
pygame.display.update()
# check if the current song has ended
if not pygame.mixer.music.get_busy():
# play the next song
current_song = (current_song + 1) % len(songs)
pygame.mixer.music.load(os.path.join(music_dir, songs[current_song]))
pygame.mixer.music.play()
Here are some useful links related creating a music player using Python and Pygame:
- Pygame documentation: https://www.pygame.org/docs/
- Pygame tutorial: https://www.pygame.org/docs/tut/index.html
- Pygame mixer module documentation: https://www.pygame.org/docs/ref/mixer.html
- Pygame display module documentation: https://www.pygame.org/docs/ref/display.html
- OS module documentation: https://docs.python.org/3/library/os.html
- Python documentation: https://docs.python.org/3/
- Free mp3 music downloads: https://www.mp3juices.cc/
- Sound effects library: https://freesound.org/
- Pygame community discussion forum: https://pygame.readthedocs.io/en/latest/community/index.html
- Online music player using Pygame: https://www.pygame.org/project/3070/6006