Thread: how to play .wav in command prompt in windows?

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    9

    how to play .wav in command prompt in windows?

    im a c beginner.

    i need my program to play wav in command prompt.

    is there a simple library that would allow me to do that with a simple function? something like

    playwav ("sample.wav");

    i searched for simmilar threads on the internet, but what i found was too complicated for my beginner level...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Which Operating system and compiler are you using?
    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.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    To answer the question in the title (how to play in a command prompt), you could use the following tool which provides a play command which does what you want

    SoX - Sound eXchange | SourceForge.net

    This is not a C library, it is a command line tool.

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    9
    im on windows 8.1 and i am using tcc (tiny c compiler)
    i know i could use an external tool to play wav in command prompt, but id like to do it with my program.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You could look into cross-platform multimedia libraries that have a C interface. Three popular ones are listed

    Allegro - Allegro - A game programming library -
    SDL - Simple DirectMedia Layer - Homepage
    SFML - SFML

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    9
    thanks salem.. but unfortunately my tiny c compiler doesnt have playsound in windows.h...
    i tried downloading gcc.. but still this
    PlaySound (TEXT("yamaha.wav"), NULL, SND_FILENAME);
    did not work..

    it seems this is beyond my beginner level... thanks anyway guys..

  8. #8
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by billyblanco View Post
    thanks salem.. but unfortunately my tiny c compiler doesnt have playsound in windows.h...
    i tried downloading gcc.. but still this
    PlaySound (TEXT("yamaha.wav"), NULL, SND_FILENAME);
    did not work..

    it seems this is beyond my beginner level... thanks anyway guys..
    You might have to link to the Winmm.dll. What errors do you get?
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You could try the following example program

    Code:
    #include <stdlib.h>
    #include <wchar.h>
    #include <Windows.h>
    #include <Mmsystem.h>
    
    int main(void)
    {
        LPCWSTR filename = L"sounds/hello.wav";
        BOOL ret = PlaySoundW(filename, NULL, SND_FILENAME | SND_NODEFAULT);
        if (!ret) {
            wprintf(L"Failed to play file ``%s''\n", filename);
            exit(1);
        }
    }
    As Alpo mentioned link with Winmm.dll e.g. for gcc you could use the following

    gcc -o demo_playsound.exe demo_playsound.c -lwinmm

  10. #10
    Registered User
    Join Date
    Oct 2014
    Posts
    9
    thanks guys

    c99tutorial, i tried your example, it says "undefined reference to "__imp_PlaySoundW".. but when i linked it to winmm.dll it worked!
    it plays wav now!
    i'm a total beginner but i like this C a lot..

    thanks!

  11. #11
    Registered User
    Join Date
    Nov 2013
    Posts
    107
    Quote Originally Posted by billyblanco View Post
    thanks guys c99tutorial, i tried your example, it says "undefined reference to "__imp_PlaySoundW".. but when i linked it to winmm.dll it worked! it plays wav now! i'm a total beginner but i like this C a lot.. thanks!
    Why would you all be saying to link with a dynamic library, when it's the static library that's needed, you need to link winmm.lib. Are you using gcc now, if so when IDE are you using like codeblocks, do you know that Microsoft have a free IDE/Compiler called Visual studio express, it's your best option.

  12. #12
    Registered User
    Join Date
    Oct 2014
    Posts
    9
    Yes, i compiled my file with gcc (i downloaded mingw), i only use text editor and command prompt compiler.. I will try Visual Studio express..

  13. #13
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by jim_0 View Post
    Why would you all be saying to link with a dynamic library, when it's the static library that's needed, you need to link winmm.lib.
    -lwinmm does link to winmm.lib, and he said that solved his problem, however I'm pretty sure even while linked to that, if he deleted winmm.dll from his system32 directory (which I don't recommend,) his program will complain that it's missing.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  14. #14
    Registered User
    Join Date
    Oct 2014
    Posts
    9
    OK, now that my sound works can I push my luck with another question?

    So now I have some .WAV files that my program plays with this command:

    LPCWSTR filename = L"sounds/testing1.wav";
    BOOL ret = PlaySoundW (filename, NULL, SND_FILENAME | SND_NODEFAULT);

    and I play the next one with:

    filename = L"sounds/testing2.wav";
    ret = PlaySoundW (filename, NULL, SND_FILENAME | SND_NODEFAULT);

    and it works perfectly!

    And I just want to know if there is a GCC command or something that would enable me to link all those .WAVs to my .EXE file?
    I see that PlaySound has a SND_MEMORY flag and I'm guessing I could use that somehow to play those linked WAVs..
    But I don't know how to link them to my .EXE.

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Something like this should work:
    Code:
    bool GetBinaryResource(int res_id, void **res)
    {
        *res = 0;
        HMODULE hmod = GetModuleHandle(0);
        HRSRC hr = FindResource(hmod, MAKEINTRESOURCE(res_id), RT_RCDATA);
        if (!hr)
        {
            LogMessage(L"FindResource(%d) failed, le = %u", res_id, GetLastError());
            return false;
        }//if
    
        DWORD hrsz = SizeofResource(hmod, hr);
        if (!hrsz)
        {
            LogMessage(L"SizeofResource() failed, le = %u", GetLastError());
            return false;
        }//if
    
        HGLOBAL hg = LoadResource(hmod, hr);
        if (!hg)
        {
            LogMessage(L"LoadResource() failed, le = %u", GetLastError());
            return false;
        }//if
    
        *res = LockResource(hg);
        if (!*res)
        {
            LogMessage(L"LockResource() failed, le = %u", GetLastError());
            return false;
        }//if
    
        return true;
    }//GetBinaryResource
    
    /*
    In your RC file:
    IDR_WAVE1 RCDATA DISCARDABLE "testing1.wav"
    */
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Windows command prompt and the WINAPI
    By Spry in forum Windows Programming
    Replies: 0
    Last Post: 07-16-2012, 02:40 AM
  2. Windows programming without command prompt
    By arckeda in forum C Programming
    Replies: 9
    Last Post: 06-04-2008, 06:09 PM
  3. Output is command dos prompt not windows/frames
    By csonx_p in forum Windows Programming
    Replies: 4
    Last Post: 04-25-2008, 02:24 AM
  4. Replies: 2
    Last Post: 12-22-2006, 08:45 PM
  5. Command Prompt Windows
    By Jaken Veina in forum Windows Programming
    Replies: 2
    Last Post: 05-02-2005, 07:19 AM