Create Music Player in Python with Output

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.

Music Player using Python

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, 
Create Music Player in Python with Output
Music Player using Python

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:

  1. Pygame documentation: https://www.pygame.org/docs/
  2. Pygame tutorial: https://www.pygame.org/docs/tut/index.html
  3. Pygame mixer module documentation: https://www.pygame.org/docs/ref/mixer.html
  4. Pygame display module documentation: https://www.pygame.org/docs/ref/display.html
  5. OS module documentation: https://docs.python.org/3/library/os.html
  6. Python documentation: https://docs.python.org/3/
  7. Free mp3 music downloads: https://www.mp3juices.cc/
  8. Sound effects library: https://freesound.org/
  9. Pygame community discussion forum: https://pygame.readthedocs.io/en/latest/community/index.html
  10. Online music player using Pygame: https://www.pygame.org/project/3070/6006

By Alan Turing

Welcome to our programming exercises for programmers challenge website! Here, you can hone your coding skills through a series of carefully curated exercises that cater to programmers of all levels. Our platform offers a variety of coding challenges, ranging from easy to hard, that allow you to practice various programming concepts and algorithms.Our exercises are designed to help you think critically and develop problem-solving skills. You can choose from a wide range of programming languages, including Python, Java, JavaScript, C++, and many more. With our intuitive and user-friendly interface, you can track your progress and monitor your performance, allowing you to identify areas for improvement.We also offer a community forum, where you can interact with other programmers, exchange ideas, and get feedback on your code. Our website is optimized for SEO, so you can easily find us through popular search engines. Join us today and take your programming skills to the next level!

Leave a Reply