Hi everyone.
I'm making a simple text rpg and battle system with C and I was just wondering if it was possible to add sounds to it. I've heard that it's possible with C++ but I'm not sure if it's possible with C as well.
Any help is greatly appreciated :)

Recommended Answers

All 9 Replies

You can use the Beep() function from the Win32 API.
Beep(82, 600);
That will sound at 82Hz for 600ms.
You can use the PlaySound() function also from Win32 to play a wave file.
You need to include the header windows.h.

I'm not quite sure of how to include the sound file because I've never attempted to include another file in my program. Here's my attampt so far:

#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <C:\Users\Imad.Imad-PC\Documents\Computer Programming\Target.wav>      //includes sound file?

int main()
{
    system("PAUSE");                                                            
    
    PlaySound(C:\Users\Imad.Imad-PC\Documents\Computer Programming\Target.wav); //.wav file
    
    system("PAUSE");
    return 0;
}
#include <windows.h>
#include <mmsystem.h>
#include <stdio.h>
#pragma comment(lib, "winmm.lib")

int main()
{
    
	PlaySound( TEXT("C:\\WINDOWS\\.file_store_32\\sound0.wav"), NULL, SND_FILENAME | SND_ASYNC);
    getchar();
    return 0;
}

This compiles in Visual C++ 2008

Actually, you don't need mmsystem.h and that pragma. You can add winmm.lib as an additional dependency.

how to play mp3 files ???

Thanks for your help Colin Mac. But I found that what I really wanted was to use mciSendString.
And to answer apurvkolte's question, you can play mp3 files by using the mciSendString command. Here's an example code: mciSendString("play C:\\TheTarget.mp3", NULL, 0, NULL);

hi....
yes it is possible in c as well c++.
you need set frequency of sound to particular character then it will produce sound what you type.for reference-package of yashwant kanetkar is suitable.
take care........bye

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.