Python MP3 Player with pygame and Crossfade
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.
Python projects · Audio playback
Start here
Read the requirements and any configuration below, then follow the usage example. Source paths below are relative to the repository root; run commands from the working directory stated in Usage. This example’s source is in MP3 Player/Console_Code/. Install any third-party packages in an isolated Python environment.
Overview
This script plays .mp3 files from a folder you specify in a shuffled order. It uses two pygame mixer channels to overlap playback and preloads the next track in a background thread. If preloading is not ready, it loads synchronously, which can cause a gap or glitch.
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 a duration for the current track
- 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) - Cleanup after the main playback loop exits;
Ctrl+Cis not handled by a surroundingtry/finally
Prerequisites and installation
pygame
mutagen
colorama
Install dependencies:
python -m pip install pygame mutagen colorama
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 |
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.
Architecture
MP3 Player/
└── Console_Code/
└── MP3Player.py # Main script
Troubleshooting and limitations
- The playlist index wraps with modulo arithmetic; the shuffled order can repeat rather than stop after one pass. A single track can also restart when it reaches the crossfade trigger.
- The default transition starts 15 seconds before the end but fades out over 5 seconds, so approximately the final 10 seconds of the outgoing track are cut off. Align the trigger and fade duration if you want a different overlap.
pygame.mixer.Soundloads whole tracks into memory; background preloading is not a streaming decoder. Large files can increase memory usage and load time.- Interrupting with
Ctrl+Ccan skip the explicit cleanup code because the main loop has no interrupt handler orfinallyblock. - 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.
Background preloading shares a single next_sound variable without a track identity or lock. Overlapping preload and synchronous fallback work can race, so the displayed filename should not be treated as proof that the matching audio buffer is playing. Unreadable tracks can also cause repeated retries in the looping playlist.
Related documentation
- 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