954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to code a program that can play sound

below is the simple source code

#include
using namespace std;

int main()
{
cout<<"Hello"<

kakilang
Newbie Poster
22 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 
#include <stdmagic>

using namespace std;

int main()
{
  magically_do_something_platform_and_implementation_specific(PLAY_MUSIC, "hello.mpg");
}

By the way, what OS and compiler are you using? :rolleyes:

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

i use windows xp and dev c++ 4.9.9.0 compiler

kakilang
Newbie Poster
22 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Not quite ZZ Top but it's a start!
The Windows Api has a little thing called Beep(), try it ...

// simple sounds via Beep(frequency_hrz, duration_ms)

#include <iostream>
#include <windows.h>   // WinApi header file

using namespace std;

int main(int argc, char *argv[])
{
  Beep(523.2511,500);  // 523.2511 hertz (C5) for 500 milliseconds
  Beep(587.3295,500);
  Beep(659.2551,500);
  Beep(698.4565,500);
  Beep(783.9909,500);
  
  cin.get(); // wait
  return 0;
}

There are nicer things like sound(), that use the sound chip and midi voices. A little tough for C++. If you want to test it, get BCX free from: http://www.rjpcomputing.com/programming/bcx/devsuite.html

get used to BCXDX and learn to fly with sound and graphics! There are lots of sample files. This goes beyond the usual boring calculator and employee database stuff! You are still learning C/C++ though.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

thx Vegaseat the program successdfully run and the beep sound is funny

kakilang
Newbie Poster
22 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Vegaseat do u know how to link the music or sound like "Hello.mpg"

when the compiler executed until cout<<"Hello"<

kakilang
Newbie Poster
22 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

you know, you can use the PlaySound function. you need to convert the audio file to a .wav file and you need to link winm.lib to your C++ file. and here is the code.

#include<windows.h>
#include<iostream>
int main(char argc)
{
    
       cout << "Hello" << endl;
       Sleep(50);
       PlaySound(TEXT("hello.wav"), NULL, SND_FILENAME);
       system("pause");
}
thecoolman5
Posting Whiz in Training
234 posts since Dec 2010
Reputation Points: 18
Solved Threads: 1
 

Post: Markdown Syntax: Formatting Help
You