Thread: Playing music

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    155

    Playing music

    Hi I need help on some code on playing music in my program. I get this error that I dont know how to fixs with my code so maybe you all can help me out. I'm just trying to figer out how this code works if my code seems a little short^^;
    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    	
    int main(void)
    {
     PlaySound("nameofsound",NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);
     getchar();
    }
    <This code has been eddit so other can see how the code works, so other can get help on playsound()>


    I'm using c++ but this is windows things so I thought I post it here; but if I am wrong on this, sorry to post my q/n right here^^;
    Last edited by adr; 12-28-2005 at 10:49 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Link winmm.lib in with your project.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Hmm, now it says "winmm.lib: No such file or directory. " I dont know if its just my compiler or not. I'm using Dev-c++ 4.9.9.2.
    Last edited by adr; 12-28-2005 at 03:09 PM.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Libray names are different with MinGW, the compiler tool set for which dev-cpp is one ide for. The libraries are prefixed with 'lib' and have an 'a' extension so, winmm.lib becomes 'libwinmm.a'. However, it's more usual to pass -lname_of_lib_without_prefix_or_extension to the linker; and that is a small case letter 'L' at the start, if you're in any doubt.

    Do the following in Dev-cpp to add that particular library to your project:

    Project menu -->project options --> parameters tab, add -lwinmm to the 'linker' field.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Ok... ; ; I am having still no luck at this. I adding the #include <libwinmm.a> to my code as seen. I still get the error say its not there and I did add "Project/Project Options/Parameters/Add Library" to it but still nothing>< grrr.
    Code:
    #include <iostream>
    #include <windows.h>
    #include <libwinmm.a>
    
    using namespace std;
    	
    int main(void)
    {
     PlaySound("C:\\WINDOWS\\02 - Balamb Garden.WAV", 0, SND_ASYNC | SND_FILENAME);
    }
    Last edited by adr; 12-28-2005 at 09:05 PM.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>I adding the #include <libwinmm.a> to my code as seen.<<

    Remove that as it's garbage; you include libraries as linker options only (unless you use ms compilers and favour #pragma masochism).

    If you have linked the required library as I have described and removed the erroneous #include, then your code should at least build. If it doesn't, you should provide us with the exact warnings or errors that are generated to better help us to help you.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    It builds, then I hear this sound, then it closes when I remove #include <libwinmm.a>.Sorry i'm just trying to get how this playsound() works>< Dont know if this is something >8:12 C:\Documents and Settings\Owner\Desktop\main.cpp [Warning] unknown escape sequence '\D'<

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by msdn
    SND_ASYNC The sound is played asynchronously and PlaySound returns immediately after beginning the sound.

    See PlaySound
    For the console disappearing immediately, read the faq.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    I was showed that it plays the sound but lets the rest of the operation keep going till it has it playing, so it doesnt stop the program.>< I'll take a look at this more. Thank you for so much help.

    I know how to stop it from closeing very easy, that was not one of the problems.
    Last edited by adr; 12-28-2005 at 10:04 PM.

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by adr
    < Dont know if this is something >8:12 C:\Documents and Settings\Owner\Desktop\main.cpp [Warning] unknown escape sequence '\D'<
    MinGW and other gnu tools, are none too fond of spaces in path names - make sure you've both installed dev-cpp to a path without spaces and that any projects you create are likewise in paths without spaces.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Oh I got 1 more q/n, sorry to ask, but what if I dont want to have a project for this. Dev will only give link to the lib if its a project. Is there a way around this use of having to link the project to the lib so I dont have to make it a project? I'm so new to this, sorry to bother so much.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You probably have to have a project. Maybe there's some little trick outlined in the Dev-C++ documentation, but most IDEs use the "project" concept to store information about a whole program you're making, when things get more complex than just a source code file. Since you're linking in another library, that's probably the case - so just use a project. They're not too difficult to use.

  13. #13
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Search the board, many people have asked this question before. (Including myself.)

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    maybe take a look at fmod? http://www.fmod.org/
    It's free for personal use. As of posting, shareware license for 1 title is $100, and commercial licenses cost quite a bit more (but may be reasonable, i'm not knowledgable about pricings and such)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is AOL music crazy?
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-24-2006, 07:24 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, 09:09 AM
  4. Playing Music in C#
    By Smoose777 in forum C# Programming
    Replies: 9
    Last Post: 06-06-2002, 04:11 PM
  5. Playing Music
    By Quantrizi in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2002, 07:47 PM