A basic, single-header C++ implementation of an SDL Mixer 1.2 WAV file audio player in SDL2. The player can play both sound effect files (Mix_Chunk*
) and music tracks (Mix_Music*
). It can also change sound effect and music volume separately, and allows you to pause and stop audio playback.
Important note: As far as I'm aware, SDL Mixer is only compatible with sound files saved in the 16-bit WAV format. If your file is not saved in that format, it may not play correctly. Audacity is capable of converting them for you, as shown below:
First ensure you have a correctly configured installation of SDL2 and SDL Mixer. The SDL_Mixer.h
file should be in the include directory of your SDL installation folder. You should also ensure you have properly set up SDL2_mixer.lib
and included the SDL2_mixer.dll
file in your project directory.
- Include the header file AudioPlayer.h.
- Edit the sound effect and music track storage objects (
SoundEffectFiles
andMusicFiles
) to contain the filepaths of your sound and music files, as well as the number of total sound effects and tracks. - On construction, the files will all be loaded as sound objects and ready for playing.