Thread: Sounds in DevC Help

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    22

    Sounds in DevC Help

    How can I used the playsound()?
    Is it even usable in DevC?

    I found many threads about it in the internet but all of them are useless I mean is I can't understand them.

    I need to put music in my project, can someone teach me how to use PlaySound() or other alternatives most preferably using DevC or GCC

    I'm flexible with the file types, I'm also ok if it's only midi files

    Cheers

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    #pragma comment(lib:"winmm.lib")
    
    int main (void)
      { 
    
        char fname[MAX_PATH] = {0};
        printf("Enter the song filename (*.wav) : ");
        fgets(fname,MAX_PATH,stdin);
        fname[strlen(fname) -1] = 0;  // remove newline.
    
        PlaySound(fname,NULL, SND_FILENAME | SND_ASYNC | SND_NOWAIT);
    
        getchar();
        return 0; 
    }
    The information for PlaySound() is... HERE
    You can look any Windows API call up... HERE

    You can also type MSDN <FunctionName> into a google search...
    Last edited by CommonTater; 10-16-2011 at 12:56 AM.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    Quote Originally Posted by CommonTater View Post
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    #pragma comment(lib:"winmm.lib")
    
    int main (void)
      { 
    
        char fname[MAX_PATH] = {0};
        printf("Enter the song filename (*.wav) : ");
        fgets(fname,MAX_PATH,stdin);
        fname[strlen(fname) -1] = 0;  // remove newline.
    
        PlaySound(fname,NULL, SND_FILENAME | SND_ASYNC | SND_NOWAIT);
    
        getchar();
        return 0; 
    }
    The information for PlaySound() is... HERE
    You can look any Windows API call up... HERE

    You can also type MSDN <FunctionName> into a google search...
    Thanks for the help, Got questions when I copy the whole thing there is an error :

    Undefined Reference to PlaySoundA@12

    Also How can I don't get what the MSDN said that's why I want to ask how to start playing the song and stop it.
    btw
    Thanks dude this is a great help

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You need to link the multimedia library on windows Winmm.lib.
    Try -lwinmm in your linker settings.
    Your problem is a windows/winapi problem not a C problem.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    Dude How to do that @_@
    Sorry I'm too dependent on default settings

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by justin777 View Post
    Dude How to do that @_@
    Sorry I'm too dependent on default settings
    It might be the #pragma that's the problem...

    try...
    Code:
    #pragma comment(lib,"winmm.lib")
    Not all compilers are totally standard on this yet. Some use a colon some use a comma.

    Otherwise get busy and learn about your compiler's linker settings... and as suggested add winmm.lib to your linker's list.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    Tater still not working
    maybe it's the linker something, do you know how can I fix it, Tried google but
    damn it's literally nosebleed. I can learn all of that but I have a Deadline to catch

    Please Help

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What compiler are you using?

    DevC++ has a full set of windows headers, so does GCC... This HAS to be a linker setting and where that is is going to depend entirely on your IDE...

    Hint: Look in the help file, already...
    Last edited by CommonTater; 10-16-2011 at 03:16 AM.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    Dude It works OMG
    It's only sad that I need to use DevC++

    I don't think there are any linker options in NotePad++


    btw I need to ask this

    Code:
    "  char fname[MAX_PATH] = {0};
        printf("Enter the song filename (*.wav) : ");
        fgets(fname,MAX_PATH,stdin);
        fname[strlen(fname) -1] = 0;  // remove newline.
    "
    Are these necessary? Because I can't understand this code (sorry still in stdio.h)
    I want is like

    Code:
     PlaySound(fname,NULL, SND_FILENAME | SND_ASYNC | SND_NOWAIT);
    *fname is the name of the file right?

    but what is the exact codes for this like in the Name, should I just put it like

    Code:
     PlaySound((Song Name),NULL, SND_FILENAME | SND_ASYNC | SND_NOWAIT);
    or there is more to it.


    Also How can I loop the song so that it's continues until the end of the program
    and
    How can I stop it??

    Dude you have been an amazing help Cheers bro

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by justin777 View Post
    btw I need to ask this

    Code:
    "  char fname[MAX_PATH] = {0};
        printf("Enter the song filename (*.wav) : ");
        fgets(fname,MAX_PATH,stdin);
        fname[strlen(fname) -1] = 0;  // remove newline.
    "
    Are these necessary? Because I can't understand this code (sorry still in stdio.h)
    Yes, they are necessary...



    I want is like

    Code:
     PlaySound(fname,NULL, SND_FILENAME | SND_ASYNC | SND_NOWAIT);
    *fname is the name of the file right?
    Correct... entered by the user through the fgets() command.

    but what is the exact codes for this like in the Name
    You're looking at them....


    should I just put it like

    Code:
     PlaySound((Song Name),NULL, SND_FILENAME | SND_ASYNC | SND_NOWAIT);
    or there is more to it.
    If you want to specify an explicit file name, rather than have the user enter it manually, you can do this...
    Code:
    PlaySound("c:\\mymusic\\waves\\noise.wav",NULL,SND_FILENAME | SND_ASYNC | SND_NOWAIT);
    of course you will need to substitute a valid pathname from your own system... and do note the double slashes... yes, they're necessary.


    Also How can I loop the song so that it's continues until the end of the program
    and
    How can I stop it??
    Y'know what... sooner or later you're going to have to learn how to look this stuff up for yourself.
    Might as well start now. I gave you the links in my first message... *use them*

    And while you're there... also look up MCI which is far more capable.
    Last edited by CommonTater; 10-16-2011 at 03:50 AM.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    Dude sorry for disturbing you but before you reply I learned everything I need. I'm to happy It's a success that I can't wait for your reply

    Lolz

    and Also you can use this

    Code:
    PlaySound(TEXT("(songname).wav"),NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
    this is for looping. and you can use this if the song is also in your file project folder..

    Man You saved my ass in another level ) Can't express my feelings in words, if you're here near me I have kiss you a while ago

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Unless you're working in unicode you don't need the TEXT() wrapper macro and for that to work the sound file has to be in the same folder as the executable.

    Yeah I know... I just did your homework for you... consider it your one and only freebie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ads with sounds
    By ober in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 03-30-2005, 11:38 AM
  2. can someone help with sounds and...
    By Nemini in forum Game Programming
    Replies: 6
    Last Post: 03-17-2005, 10:55 AM
  3. Sounds
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-17-2003, 06:27 AM
  4. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 03:34 PM
  5. Sounds, or no sounds?
    By face_master in forum C++ Programming
    Replies: 3
    Last Post: 09-03-2001, 05:29 PM