Hello there,

i am new at C++ and i would like to learn a few thing if you could help me.

I would like to learn how to load sounds on C++. I mean having the sound file on my computer and input it on the exe program i create via Borland C++. Can i do that with a midi file ? What i really want is to run the exe program and on the background to listen to the midi file i loaded on the program. Thanks for any help.

Dani AI

Generated

asked how to load a sound file into a Borland C++ program and play a MIDI in the background. As pointed to a snippet, here is a concise, practical summary of safe options and a ready example for Windows.

On Windows the simplest choices are:

  • For WAV files: PlaySound from WinMM (easy, can play embedded resources).
  • For MIDI files: the MCI command interface (mciSendString) is the easiest way to play sequenced MIDI in the background.
    Include <mmsystem.h> and link against winmm.lib in the project settings. PlaySound does not handle MIDI; use MCI for MIDI (documentation: PlaySound and mciSendString).

Example (open, play looped MIDI, then stop/close):

#include <windows.h>
#include <mmsystem.h>

/* open and play MIDI file in background and loop */
mciSendString("open \"C:\\path\\to\\music.mid\" type sequencer alias bgm", NULL, 0, NULL);
mciSendString("play bgm repeat", NULL, 0, NULL);

/* stop and close when exiting */
mciSendString("stop bgm", NULL, 0, NULL);
mciSendString("close bgm", NULL, 0, NULL);

Embedding a sound inside the EXE: WAV resources can be played directly with PlaySound(..., hInstance, SND_RESOURCE | SND_ASYNC). MIDI resources typically must be extracted at runtime to a temporary file (use FindResource/LoadResource/LockResource and write to disk) and then opened by MCI. For consistent audio across machines, convert MIDI to WAV/OGG/MP3 (MIDI relies on the OS synth and soundfonts, so playback may vary).

Troubleshooting notes: verify full path quoting, check mciSendString return values and use mciGetErrorString to get human messages, and ensure winmm.lib is linked. For cross-platform or higher-level control consider SDL_mixer (simple API for many formats) — see the SDL_mixer project page for details.

Recommended Answers

All 2 Replies

Thank you very much. I will try it if i have anything i will be back thanks !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.