Playing Music

This is a discussion on Playing Music within the C Programming forums, part of the General Programming Boards category; I wanted to create a music player (really basic one at best), but I don't really know how to code ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32

    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;
    }

  2. #2
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    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

  3. #3
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    "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.
    Last edited by msh; 11-26-2010 at 01:21 AM. Reason: Fixed poor spelling

  4. #4
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    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.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32
    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.
    Last edited by patrink; 11-26-2010 at 04:06 PM. Reason: error in code provided

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Playing music
    By adr in forum Windows Programming
    Replies: 13
    Last Post: 12-31-2005, 03:23 PM
  2. playing music in C++
    By dutch's finest in forum C++ Programming
    Replies: 3
    Last Post: 02-21-2005, 05:57 PM
  3. Playing music
    By PJYelton in forum Windows Programming
    Replies: 5
    Last Post: 06-25-2003, 10:09 AM
  4. Playing Music in C#
    By Smoose777 in forum C# Programming
    Replies: 9
    Last Post: 06-06-2002, 05:11 PM
  5. Playing Music
    By Quantrizi in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2002, 08:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21