How to easily and quickly read/play an audio file in C++

Hello! I’m Xavier Jouvenot and in this small post, we are going to see how to easily and quickly read/play an audio file in C++.

Self promotion: You can find other articles on my website 😉


In C++, there are sadly some operations that require a lot of boilerplate code to do some simple operations. Playing a audio file is on of those operation that can be difficult to do natively in C++, whereas in some other language such as Python or Javascript.

But the more the C++ language goes forward with its new releases and the more the C++ tools progress, the more those operations become simpler to do. And with that in mind, when I tried to play a sound file in a simple terminal application and didn’t find some valid article or online solution, I decided to write about it, and this is what this article is about, and what we are going to see right now.


First of all, let’s talk about the tools we are going to use in this solution:

  • Conan, an open-source package manager

with those tools, we can, with two files, generates a program able to play an audio file.

First, we need to specify to conan that we want to integrate the sfml library, and more specifically its audio module, via the conanfile.txt file:

[generators] cmake_multi

Then, with the CMakeLists.txt file, we are going to bind all those tools:

cmake_minimum_required(VERSION 3.18)
# Get the Conan executable
NAMES conan conan.exe
DOC "Conan executable" REQUIRED)
# Install the conan packages and specify the audio module of the
# sfml library installation
COMMAND ${CONAN_EXE} install . --install-folder=build --build=missing -s build_type=Debug -o sfml:audio=True
COMMAND ${CONAN_EXE} install . --install-folder=build --build=missing -s build_type=Release -o sfml:audio=True
# Create the target
add_executable(program_name src/main.cpp)
set_property(TARGET program_name PROPERTY CXX_STANDARD 17)
# Links the target and the sfml library
target_link_libraries(program_name PUBLIC CONAN_PKG::sfml)

And finally, in a few C++ lines, we can play a specific audio file:

#include <SFML/Audio.hpp>
#include <cassert>
#include <filesystem>
#include <iostream>
#include <thread>
int main() {
sf::Music music;
std::filesystem::path p ("C:/absolute/path/to/the/audio/file.wav");
// Wait for the music to finish, or the program would finish before hearing any sound.
return 0;

This is, at the time I am writing this article, the smallest project I have found to be able to play an audio file in a cross platform executable. If you have any other solution or improvement, please share it with me, I will be more than happy to read it 🙂

Thank you all for reading this article, And until my next article, have a splendid day 😉

Interesting links

Originally published at on November 25, 2020.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store