Python MP3 Player with Crossfade — pygame Console Music Player
A console-based MP3 player written in Python with crossfade transitions between tracks, background preloading of the next song, and shuffled playback, built on pygame.mixer and mutagen.
Overview
This script plays all .mp3 files from a folder you specify, in shuffled order, with smooth crossfade transitions between tracks. It uses pygame’s dual-channel mixer to overlap the end of one track with the start of the next, preventing any silence between songs. The next track is preloaded in a background thread so the transition is seamless.
Features
- Shuffle playback of all
.mp3files in a given folder - Crossfade between tracks (configurable duration and trigger time)
- Background preloading of the next track via a daemon thread
- Animated “Playing…” indicator in the console
- Displays previous, current, and next track names with durations
- Console window title updated to show the currently playing track
- Windows system-sleep prevention while music plays (
SetThreadExecutionStatewithES_SYSTEM_REQUIRED; the display can still turn off) - Graceful shutdown on playlist end or interrupt
Configuration
Two constants in the script control the crossfade behavior:
| Constant | Default | Description |
|---|---|---|
CROSSFADE_MS |
5000 |
Duration of the fade effect in milliseconds (5 seconds) |
CROSSFADE_START_TIME_S |
15 |
How many seconds before a song ends to begin the crossfade |
Requirements
pygame
mutagen
colorama
Install dependencies:
pip install pygame mutagen colorama
Usage
From the Console_Code folder:
python MP3Player.py
When prompted, enter the full path to your music folder:
Enter music folder path: C:\Music\Playlist
The player will shuffle the playlist, preload the first track, and begin playback automatically.
How It Works
- All
.mp3files in the folder are discovered and shuffled. - The pygame mixer is initialized with two channels.
- Before the main loop starts, the first track is preloaded in a background thread.
- When a track is playing, a second background thread preloads the next one.
- When
CROSSFADE_START_TIME_Sseconds remain, the inactive channel fades in the next track while the active channel fades out. - Channels swap roles after each transition.
File Structure
MP3 Player/
└── Console_Code/
└── MP3Player.py # Main script
Notes
- Only
.mp3files are supported. - If a track fails to load, it is skipped and the next track loads.
- On non-Windows systems, the console title is set with an ANSI escape sequence; sleep prevention is only available on Windows.
- Track lengths are read with
mutagenso the player knows when to start the crossfade.
Related Projects
- MP3 Tags — read ID3 metadata
- Improve MP3 Quality — re-encode MP3 files
- Audio8D — 8D audio converter
- Back to all Python projects
Documentation links
Read all Python Projects and Code Examples guides online · Project overview · All developer projects