hi, i have written this code.

#include<iostream>
#include<windows.h>
#include<mmsystem.h>
#include<stdlib.h>
using namespace std;
int main (char argc)
{
      PlaySound("TardisTest1.wav", NULL, SND_FILENAME | SND_ASYNC);
      system("pause");
      return 0;
}

and for some reason, when the program runs, all it does is system("pause); and it doesnt play the sound. I have my sound file included in the folder of my project. any solutions?

Dani AI

Generated

Good call by on the Unicode literal. If you want a quick, minimal sanity check with the WinMM API, call the wide version directly, link against Winmm, and verify the return value. Also note that PlaySound searches only a few directories (current dir, Windows dirs, PATH, etc.) and the input path is limited to 256 chars; long or relative paths can cause silent failure or the default sound. PlaySound docs.

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

// Build with MinGW: g++ main.cpp -lwinmm
int main() {
    const wchar_t* wav = L"C:\\full\\path\\TardisTest1.wav";
    if (!PlaySoundW(wav, NULL, SND_FILENAME | SND_ASYNC)) {
        std::cerr << "PlaySound failed (file missing/unsupported).\n";
    }
    std::cin.get();
}

For your follow‑ups: PlaySound is for simple waveform playback; it does not decode OGG, does not mix multiple sounds, and offers no pitch/speed control. Use an audio library. SFML is a friendly option: it loads OGG, lets you play multiple sounds at once, and supports pitch (which changes speed). Example:

#include <SFML/Audio.hpp>

sf::SoundBuffer buf;
buf.loadFromFile("TardisTest1.ogg");   // OGG supported
sf::Sound a(buf), b(buf);
a.setPitch(1.2f);                      // ~20% faster
a.play(); b.play();                    // simultaneous

See the SFML audio tutorial for supported formats and usage, including streaming .ogg via sf::Music. SFML audio tutorial. If you prefer a Windows‑only low‑level route with fine‑grained speed control, XAudio2 exposes SetFrequencyRatio to change playback rate/pitch. IXAudio2::SetFrequencyRatio.

Recommended Answers

All 11 Replies

Try ensuring that you're passing the right kind of variable, by wrapping the string as follows:

PlaySound(TEXT("TardisTest1.wav"), NULL, SND_FILENAME | SND_ASYNC);

The project directory is not guaranteed to be the working directory of the executable. Try giving an absolute path (e.g. "C:/TardisTest1.wav").

Also, PlaySound gives a return value. Check that value to see if the function was successful.

Do you hear any sound at all? If the function can't find the file, you should hear the default sound.

ok, that got it to work. now, is there any way to play an ogg audio file?

Well, I have been using DEV-C++ ever since I was 10 years old and I don't want to give up on it now. But thanks for helping me.

"I have been using DEV-C++ ever since I was 10 years old"

That makes me wince. We don't say it's bad just for the fun of it. It really is bad. Just because you've learned how to work around all the problems doesn't mean you can't get something better.

Well, I would rather prefer to stick to what I'm use to.

Of course, of course. It's entirely your choice to use, for example, a compiler that is not C++ standard compliant. Be aware that if you ever have to work with another compiler, you might find that your code stops working. It is possible to continue using Dev-C++ and swap out the compiler for a better one, so that's always an option.

Ok.

One more thing, how do you get the audio to speed up?

And how do you get it to play two songs at the same time?

You can't do that with PlaySound. If you want that kind of functionality you need a library like OpenAL or SDL or SFML etc...

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.