Introduction

The purpose of this application is to create a simple music player using the C++ programming language.
The goal is to play audio files using basic features like opening a file, playing the file, and stopping it.
While this implementation will be very basic, it will provide an understanding of how to interact with
audio libraries and manage resources to play music programmatically.

Objective

The objective of this program is to:

  • Load and play an audio file.
  • Provide simple controls like play, pause, and stop.
  • Learn how to use external libraries like SFML for audio playback.

C++ Program Code

#include <SFML/Audio.hpp>
#include <iostream>
#include <string>

int main() {
    // Create a music object
    sf::Music music;
    
    // Load a music file
    if (!music.openFromFile("song.ogg")) {
        std::cerr << "Error: Unable to load the music file!" << std::endl;
        return -1;
    }

    // Play the music
    music.play();
    std::cout << "Music is now playing. Press Enter to stop..." << std::endl;
    
    // Wait for the user to press Enter to stop the music
    std::cin.get();
    
    // Stop the music
    music.stop();
    std::cout << "Music stopped. Exiting program." << std::endl;

    return 0;
}

Explanation of Program Structure

1. We include the SFML library which provides support for audio functions.
2. We create an object of type sf::Music that will handle the music.
3. The music file (in .ogg format) is loaded using openFromFile.
If the file is not found, an error message is displayed.
4. The music is played using the play() method, and the program waits for the user to press Enter to stop it.
5. After the user presses Enter, the stop() method stops the music and the program ends.

How to Run the Program

To run this program, follow these steps:

  1. Install SFML (Simple and Fast Multimedia Library) on your system. You can find the installation guide on the official SFML website: SFML Download.
  2. Make sure you have a C++ compiler like GCC or MSVC installed on your system.
  3. Save the code to a file, e.g., music_player.cpp.
  4. Compile the program using the following command:
    g++ -o music_player music_player.cpp -lsfml-audio -lsfml-system
  5. Run the compiled program:
    ./music_player
  6. Ensure you have a valid audio file (e.g., song.ogg) in the same directory as the program.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)