-
Playing Music
I wanted to create a music player (really basic one at best), but I don't really know how to code one. I have searched google a bit and found that
Code:
PlaySound(TEXT("filename.wav"), NULL, SND_FILENAME);
could work (from PlaySound Function (Windows)) but when ever I compile it, it seems to give me errors (using GCC, it says undefined reference to `PlaySoundA@12').
MSDN doesn't give too much information and I don't know exactly what is going wrong.
This is the code I am using currently: (also, i know its REALLY basic, but I can't even get the song to work, so I didn't work on anything else)
Code:
#include <stdio.h>
#include <Windows.h>
char songlist[255][255] = {"song1.wav", "song2.wav", "song3.wav" };
int i = 0, id = 0;
int music(int id)
{
PlaySound(TEXT(songlist[id]), NULL, SND_FILENAME);
return 0;
}
int main(int argc, char** argv)
{
printf("Songs to choose from are:\n");
for (i = 1; songlist[i] != NULL ; i++)
{
printf("%d) %s", i,songlist[i]);
}
printf("\nWhich song would you like to choose?");
scanf("%d", &id);
if (id > i || id < 0)
{
printf("invalid choice\n");
getchar();
main(argc, argv);
}
else
{
music(id);
}
return 0;
}
-
Greetings and Happy Turkey Day! :)
That is a fine concept there though it has errors.
Arrays start at 0, not at i = 1.
songlist[i] will never == NULL because the array is not terminated with 0
songlist[i] will still never==NULL because it is not what it appears to be.
if id > i is off by 1 because the loop shall terminate at the first empty string
-
"undefined reference to some_function" usually means that you are not linking in the library whose functions you're using. Including the header is not enough for non-Standard functions.
In this case, I believe you need to add the following to your build command (that's dash ell, not dash one).
Code:
-l winmm
F.ex
gcc main.c -Wall -o ./bin/main -l winmm
Try it, and if it doesn't work, post your entire build command and what error your got.
-
If you are using an IDE add "winmm" to the linker in project options.
You might also want to check out the music section in Charles Petzold's book, Programming Windows. You can find the code online I believe.
-
While playsound can be used for music, it's pretty impractical. .WAV files of anything more than a couple of seconds long are absolutely huge!
You might be a lot happier looking into the MCI subsystem in windows. It will allow you, with very simple commands, to play just about any media type windows media player can play... including MP3, AVI, MIDI and others.
MCI (Windows)
If you combine this with the Windows GetOpenFilename() dialog, your user can pick his own music from libraries of his own creation.
Play with it some more and you can give him transport controls for play, pause, stop etc. very easily.
-
hellork, thanks for reminding me that i forgot the following code:
Code:
/*...*/
printf("%d) %s", i,songlist[i - 1]);
/*...*/
music(id -1);
/*...*/
I wanted to re-use the same number to make things just a bit easier, but i forgot to minus the one that I added. One question though, How would i NULL terminate an array for my for()?
msh, thanks for the code to link the library using GCC. Working as intended now
And thanks CommonTater Ill look into it.