PlaySound error

This is a discussion on PlaySound error within the C++ Programming forums, part of the General Programming Boards category; I keep getting an error message when i try to compile this code could someone please help me? Code: #include ...

  1. #1
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106

    PlaySound error

    I keep getting an error message when i try to compile this code could someone please help me?
    Code:
    #include <iostream>
    #include <iomanip>
    #include <windows.h>
    
    using namespace std;
    
    int main(void)
    {
        cout<<"Luke Wilkinson's MP3 Player" << endl;
        cout<<endl;
        cout<<setfill('-')<<setw(80)<<"-"<<endl;
        cout<<endl;
        cout<<"Press enter to load song."<<endl;
        cin.get();
        PlaySound("test.wav", 0, SND_ASYNC | SND_FILENAME);
        cin.get();
    }
    OS=XP
    Compile=Dev-C++

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    > I keep getting an error message when i try to compile this code could someone please help me?
    Well paste your error messages then.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    [Linked error]undefined reference to 'PlaySoundA@12'
    id returned 1 exit status

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    http://msdn.microsoft.com/library/de..._playsound.asp
    Right at the end, it says
    Library: Use Winmm.lib.

    So you need to link with this library.

    Depending on how you tell your IDE to search additional libraries, you need to configure something in the "linker" section.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    I tried that but still get the same error message.Thanks though, much appreciated.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    on devc++ you should configure the linker in 'project / options / params / linker'. add there the path to the mm library, note that on devcpp the libraries are *.a

    niara

  7. #7
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Right i did what u said but now it makes the .exe file but it doesn't run or it does but doesn't do anything and i've tried to navagate to the folder in the command prompt and run it that way but that doesn't work. Any help would be much appreciated, Thanx.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Maybe the program can't find test.wav. Try including the full path-name.

    How long is test.wav? Try a short file like the ding.wav that comes with windows (in the media folder.) I read somewhere that when PlaySound() is playing a WAV file, the file is loaded into a RAM buffer first.

    I couldn't find the file-size limit just now, but when I once tried to play a "ripped" song with PlaySound(), it was too big.

  9. #9
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    I made a few alterations could you tell me whats the matter with it. I keep getting an error message saying "error: cannot convert `std::string' to `const CHAR*' for argument `1' to `BOOL PlaySoundA(const CHAR*, HINSTANCE__*, DWORD)'".

    Code:
    #include <iostream>
    #include <iomanip>
    #include <windows.h>
    
    
    using namespace std;
    
    int main(void)
    {
        string a;
        {
        cout<<endl;
        cout<<setfill('-')<<setw(80)<<"-"<<endl;
        cout<<endl;
        cout<<"Please enter a directory of a .wav file to play:";
        getline(cin, a);
        PlaySound(a);
        cin.get();
        }
        return 0;
    }

  10. #10
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    put this instead of a:
    &a.c_str()
    My Website
    010000110010101100101011
    Add Color To Your Code!

  11. #11
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I think what mrafcho001is trying to tell you, is that the WinAPI functions use C-style strings (null-terminated character arrays) rather than C++ style string objects.

    const CHAR* is a pointer to a character (or character-array, in this case).

    And FYI - The backslash is an escape code, so… if I’m remembering correctly…. you need to include two of them: “C:\\My Documents\\test.wav”

  12. #12
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by mrafcho001
    put this instead of a:
    &a.c_str()
    Please don't do this; it breaks the const-ness of the std::string's c-string array and you have no guarantee that the current or any future implementation of any particular windows api function will not attempt to make alterations to that string.

    If PlaySound wants a non-const string then you'll have to convert it to that form. Here's one possible way:
    Code:
    std::size_t length=a.length();
    std::vector<char> non_const_str(length+1,'\0');
    a.copy(&non_const_str[0],length);
    PlaySound(&non_const_str[0],hInst,flags);
    Where hInst and flags are the relevant parameters as described for the PlaySound api function. If you're using msvc2005 use of std::string::copy may give rise to a false warning - refer to this thread for details.

    edit:

    >>The backslash is an escape code, so… if I’m remembering correctly…. you need to include two of them: “C:\\My Documents\\test.wav”<<

    Your memory serves you very well. I think PlaySound and other winapi functions requiring a path as a parameter will also accept forward slashes in the path - ie. “C:/My Documents/test.wav”.
    Last edited by Ken Fitlike; 05-15-2006 at 06:15 PM. Reason: editing and typos
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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