944,092 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 19202
  • C++ RSS
Oct 27th, 2004
0

How to code a program that can play sound

Expand Post »
below is the simple source code

#include <iostream>
using namespace std;

int main()
{
cout<<"Hello"<<endl;
system("pause");
return 0;
}??

how to let program play some music when the program
print the word "Hello"??
Let's say the song file name is "hello.mpg".
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kakilang is offline Offline
22 posts
since Oct 2004
Oct 27th, 2004
0

Re: How to code a program that can play sound

C++ Syntax (Toggle Plain Text)
  1. #include <stdmagic>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. magically_do_something_platform_and_implementation_specific(PLAY_MUSIC, "hello.mpg");
  8. }
By the way, what OS and compiler are you using? :rolleyes:
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 27th, 2004
0

Re: How to code a program that can play sound

i use windows xp and dev c++ 4.9.9.0 compiler
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kakilang is offline Offline
22 posts
since Oct 2004
Oct 27th, 2004
0

Re: How to code a program that can play sound

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

C++ Syntax (Toggle Plain Text)
  1. // simple sounds via Beep(frequency_hrz, duration_ms)
  2.  
  3. #include <iostream>
  4. #include <windows.h> // WinApi header file
  5.  
  6. using namespace std;
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. Beep(523.2511,500); // 523.2511 hertz (C5) for 500 milliseconds
  11. Beep(587.3295,500);
  12. Beep(659.2551,500);
  13. Beep(698.4565,500);
  14. Beep(783.9909,500);
  15.  
  16. cin.get(); // wait
  17. return 0;
  18. }
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/programm.../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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 28th, 2004
0

Re: How to code a program that can play sound

thx Vegaseat the program successdfully run and the beep sound is funny
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kakilang is offline Offline
22 posts
since Oct 2004
Oct 28th, 2004
0

Re: How to code a program that can play sound

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

when the compiler executed until cout<<"Hello"<<endl; and play some sound or music
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kakilang is offline Offline
22 posts
since Oct 2004
Jun 7th, 2011
0
Re: How to code a program that can play sound
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.
C++ Syntax (Toggle Plain Text)
  1. #include<windows.h>
  2. #include<iostream>
  3. int main(char argc)
  4. {
  5.  
  6. cout << "Hello" << endl;
  7. Sleep(50);
  8. PlaySound(TEXT("hello.wav"), NULL, SND_FILENAME);
  9. system("pause");
  10. }
Last edited by thecoolman5; Jun 7th, 2011 at 8:50 pm.
Reputation Points: 18
Solved Threads: 1
Posting Whiz in Training
thecoolman5 is offline Offline
233 posts
since Dec 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem with fstream write() function
Next Thread in C++ Forum Timeline: [split] from snake game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC