Noxter Noxter - 1 year ago 81
C Question

C - Is playing music possible?

I am making a text-based adventure game in language C.
I am using CodeBlocks and the game will be a console application.

Is it possible to put a "background music" thingy in the program?
It would make the game much more catchy.

I only know that the music should be in .wav format.
But I have only found C# and C++ solutions for this problem (and the C++ solution was a video on YouTube with very bad quality).

So, if it is possible in C, then can someone tell me how to do it?

It would be greatly appreciated!
(also, I would like the music to replay itself, since it is only 2 minutes long, but I want it to play over and over as long as the program is running).

Thanks for the help in advance!

Answer Source

There are no portable ways to play the music in C, which means there are very different solutions for DOS, linux, mac and windows. Here is a quick overview of the methods. All of them are somewhat complex:

  • DOS: direct PIT programming via 0x42/0x43 from timer interrupt
  • Linux: you need either alsa or pulseaudio libraries, and a background thread to play music. Or just launch media player like sox in the background, but then you must ensure user has it installed.
  • Mac OS: I have no idea. I know you can spawn open file.wav to play file in the default music player, but this will likely be very annoying to user.
  • windows: Good old MCI commands probably require the least number of lines of code. You will have to create a hidden window for MCI interfaces to work.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download