Building SDL2 from Source (GCC and Make)

Audio Playback with SDL2

How can I play audio files using SDL2?

Vector illustration representing computer programming

SDL2 provides audio playback capabilities through the SDL_mixer library, which is an extension library that simplifies audio playback and mixing. Here's an example of how to play an audio file using SDL2 and SDL_mixer:

#include <SDL.h>
#include <SDL_mixer.h>

int main(int argc, char* argv[]) {
  SDL_Init(SDL_INIT_AUDIO);
  Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT,
    2, 2048);

  Mix_Music* music{
    Mix_LoadMUS("path/to/your/audio/file.mp3")};
  if (music == nullptr) {
    // Error handling: audio file not found
    // or invalid format
    SDL_Quit();
    return 1;
  }

  // Play the music indefinitely (-1 loops)
  Mix_PlayMusic(music, -1);

  // Your event handling code goes here
  // ...

  Mix_FreeMusic(music);
  Mix_CloseAudio();
  SDL_Quit();

  return 0;
}

In this example, we first initialize the audio subsystem using SDL_Init(SDL_INIT_AUDIO). Then, we open the audio device with the desired parameters using Mix_OpenAudio. The parameters specify the sample rate (44100 Hz), audio format (MIX_DEFAULT_FORMAT), number of channels (2 for stereo), and chunk size (2048 bytes).

Next, we load the audio file using Mix_LoadMUS, which supports various audio formats such as MP3, WAV, and OGG. Make sure to provide the correct path to your audio file. If the file is not found or has an invalid format, Mix_LoadMUS will return nullptr, and we handle the error accordingly.

To play the loaded audio, we use Mix_PlayMusic, passing the loaded music and the number of loops (-1 for infinite looping). The music will start playing immediately.

After playing the music, we need to handle the necessary events and perform any other desired actions in your application.

Finally, we free the loaded music using Mix_FreeMusic, close the audio device with Mix_CloseAudio, and quit the SDL subsystems.

Remember to link against the SDL2 and SDL_mixer libraries when compiling your code. You may need to install the SDL_mixer library separately if it's not included with your SDL2 installation.

Note: The SDL_mixer library supports playing multiple audio files simultaneously and provides functions for controlling playback, adjusting volume, and applying audio effects. Refer to the SDL_mixer documentation for more advanced audio playback features.

Answers to questions are automatically generated and may not have been reviewed.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 27 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved