locked
PlaySound RRS feed

  • Question

  • #include <windows.h>
    std::wstring soundfile("myDirectory\\mySoundDirectory\\sound.wav");
    PlaySound(soundfile, NULL, SND_ASYNC);

    Why doesn't the above call to PlaySound() does not work?

    Sunday, January 20, 2013 4:12 PM

Answers

  • "How about this code? this code 'even' compiles :)"

    Nope, it doesn't. But for this particular case I can make another guess: your program exits before the sound is played, you're using SND_ASYNC after all...

    And you forgot more than to include <string>. The following code compiles and plays a sound on my computer. If this code doesn't work for you then it's either because the path to the wav file is not valid or because the wav file itself is not supported by PlaySound.

    #include <windows.h>
    #include <string>
    
    #pragma comment(lib, "winmm.lib")
    
    int main() {
        std::wstring soundfile(L"C:\\Windows\\Media\\ding.wav");
        PlaySound(soundfile.c_str(), NULL, SND_ASYNC);
        getchar();
        return 0;
    }

    "you should stay with books, some books even have pictures, ya! try those!"

    Thanks, I don't need books, I know what I'm doing. It seems to me that the advice was actually intended for yourself.


    • Edited by Mike Danes Sunday, January 20, 2013 6:29 PM bad path
    • Proposed as answer by Lazylamb Monday, January 21, 2013 10:27 AM
    • Marked as answer by ArbolOne Monday, January 21, 2013 2:56 PM
    Sunday, January 20, 2013 6:26 PM

All replies

  • Maybe because it can't find the file? Anyway, the code you have shown doesn't even compile so I can only speculate about possible reasons...
    Sunday, January 20, 2013 4:27 PM
  • How about this code? this code 'even' compiles :)

    #include <windows.h>
    int main(){
    std::wstring soundfile("./sound/brh.wav");
    PlaySound(soundfile, NULL, SND_ASYNC);
    return 0;
    }


    One more thing, I forgot to include the <string> header file, you think you could do it yourself or should I repost the code adding that one line? If you wanna do it yourself and need help, you could try C++ books or the internet, I am sure you can get help there. One thing though, in the internet you will get segmented code showing the problem, not the entire source code, so... you should stay with books, some books even have pictures, ya! try those!
    • Edited by ArbolOne Sunday, January 20, 2013 6:32 PM
    Sunday, January 20, 2013 6:13 PM
  • "How about this code? this code 'even' compiles :)"

    Nope, it doesn't. But for this particular case I can make another guess: your program exits before the sound is played, you're using SND_ASYNC after all...

    And you forgot more than to include <string>. The following code compiles and plays a sound on my computer. If this code doesn't work for you then it's either because the path to the wav file is not valid or because the wav file itself is not supported by PlaySound.

    #include <windows.h>
    #include <string>
    
    #pragma comment(lib, "winmm.lib")
    
    int main() {
        std::wstring soundfile(L"C:\\Windows\\Media\\ding.wav");
        PlaySound(soundfile.c_str(), NULL, SND_ASYNC);
        getchar();
        return 0;
    }

    "you should stay with books, some books even have pictures, ya! try those!"

    Thanks, I don't need books, I know what I'm doing. It seems to me that the advice was actually intended for yourself.


    • Edited by Mike Danes Sunday, January 20, 2013 6:29 PM bad path
    • Proposed as answer by Lazylamb Monday, January 21, 2013 10:27 AM
    • Marked as answer by ArbolOne Monday, January 21, 2013 2:56 PM
    Sunday, January 20, 2013 6:26 PM