[EDIT] Free Friday 1/9/26

Today, I watched a video and created a simple MP3 player using Python. I learned how to load and play audio files from this video. The program runs in the terminal and lets the user select songs from a folder and control them while they play.

   def play_music(folder, song_name):

    file_path = os.path.join(folder, song_name)

    if not os.path.exists(file_path):
        print("File not found")
        return

    pygame.mixer.music.load(file_path)
    pygame.mixer.music.play()

    print(f"\nNow playing: {song_name}")
    print("Commands: [P]ause, [R]esume, [S]top")

^This function is responsible for playing the selected MP3 file. It builds the file path, then checks if the song exists, thenloads it into the pygame mixer, and starts the song. It also displays the controls the user can use.

while True:
    command = input("> ").upper()

    if command == "P":
        pygame.mixer.music.pause()
        print("Paused")
    elif command == "R":
        pygame.mixer.music.unpause()
        print("Resumed")
    elif command == "S":
        pygame.mixer.music.stop()
        print("Stopped")
        return
    else:
        print("Invalid command")

^This loop listens for user input while the music is playing. It allows the user to pause, resume, or stop the song from playing. The loop keeps running until the user decides to stop the song.

Leave a Reply

Your email address will not be published. Required fields are marked *