Thread: Adding sound to console apps

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    49

    Adding sound to console apps

    How do you play sounds in a console app. I don't care if wave or mp3, or both. I can always convert...

    Id like to be able to play gun shots and music in a console app, and sometimes at the same time if so.

    Tahnx.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Look up PlaySound() on MSDN.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    It says this, but isnt clear on what file types and weather or not to set a path.

    Also I want to keep it normal C syntax. And do I include anything?

    http://msdn.microsoft.com/library/de.../PlaySound.asp

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    That's not the right playsound it's here
    http://msdn.microsoft.com/library/de..._playsound.asp

    And it accepts .wav files

    If you meant that you wanted to stick to standard c then you're out of luck since the c standard doesn't support sound.
    Last edited by Quantum1024; 05-22-2006 at 08:33 PM.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    Ok this compiles but it does nothing. Well of course it doesnt, i dont even think I have a file called this, but can anyone explain what this really means in each line. Like how would I load a .wav file from ANYWHERE in the computer?
    Code:
    #include <Mmsystem.h>
    #include <iostream>
    
    #pragma comment(lib,"Winmm.lib")
    
    
    PlaySound("Music", 0, SND_RESOURCE | SND_NODEFAULT);

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    According to the docs, PlaySound will search the following directories for the file.

    1. The directory where an application is loaded.
    2. The current directory.
    3. The Windows system directory.
    4. The 16-bit Windows system directory.
    5. The Windows directory.
    6. The directories that are listed in the PATH environment variable.
    Need any more? Or do you just mean like, specifying the direct path to a file on the computer? In that case, you can say that the file would be something like "C:\\Blah\\Blue\\Blee\\Brew.wav"

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    yes but what does the 0 mean in "Music",0

    and also is "music" just mean the title of the file I want it to find?

    also where would I exactly define where I want for it to find the file?

    sorry im new.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Where "music" is you would put your filename for example "sound.wav". The second paramater can be left as 0 as it is for playing a sound from a resource inside an executable module. The third should use SND_FILENAME if you want to play from a file SND_RESOURCE plays a sound from a resource
    Code:
    PlaySound("C:\\Sound.wav", 0,  SND_FILENAME | SND_NODEFAULT);

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Read the PlaySound documentation.

    PlaySound

    The PlaySound function plays a sound specified by the given filename, resource, or system event. (A system event may be associated with a sound in the registry or in the WIN.INI file.)

    BOOL PlaySound(
    LPCSTR pszSound,
    HMODULE hmod,
    DWORD fdwSound
    );

    Parameters

    pszSound

    A string that specifies the sound to play. The maximum length, including the null terminator, is 256 characters. If this parameter is NULL, any currently playing waveform sound is stopped. To stop a non-waveform sound, specify SND_PURGE in the fdwSound parameter.

    Three flags in fdwSound (SND_ALIAS, SND_FILENAME, and SND_RESOURCE) determine whether the name is interpreted as an alias for a system event, a filename, or a resource identifier. If none of these flags are specified, PlaySound searches the registry or the WIN.INI file for an association with the specified sound name. If an association is found, the sound event is played. If no association is found in the registry, the name is interpreted as a filename.

    hmod

    Handle to the executable file that contains the resource to be loaded. This parameter must be NULL unless SND_RESOURCE is specified in fdwSound.

    fdwSound

    Flags for playing the sound. The following values are defined.

    ...
    In your case, first param would be a filename, second is NULL or 0, third would be SND_FILENAME | SND_NODEFAULT specifying that it is a filename and that a default should not be played.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    Ok it played, but it freezes all of my actions.

    I want to be able to type in my _getche()'s while it plays, instead it pauses the program, and it DOESNT even play ALL of the file, only like dirst 20 secs of it.

    Help be appreciated thanx.

    Code:
    PlaySound("C:\\Finale.wav", 0,  SND_FILENAME | SND_NODEFAULT);

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Add the SND_SYNC flag.

  12. #12
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    SND_ASYNC*

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    Ok, that works, but lets say I wanted to play two .wav files, or even more... at the same time?

    Code:
    PlaySound("C:\\Wipala.wav", 0,  SND_FILENAME | SND_NODEFAULT | SND_ASYNC);
     PlaySound("C:\\Finale.wav", 0,  SND_FILENAME | SND_NODEFAULT | SND_ASYNC);

    And also lets say I want to STOP a sound at will?

    I've tryed various possiblities of the MSDN, but whats the best way? thanx, this will be useful.

  14. #14
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>want to STOP a sound at will

    pszSound

    A string that specifies the sound to play. The maximum length,
    including the null terminator, is 256 characters. If
    this parameter is NULL, any currently playing waveform sound is
    stopped
    . To stop a non-waveform sound, specify
    SND_PURGE in the fdwSound parameter.
    >>I wanted to play two .wav files

    Sciences are vain and full of errors, which do not spring from
    experiment, the source of all certainty.

    Leonardo DaVinci

    [edit]
    Dosn't this belong on the windows board?
    [/edit]
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    I have read that playing two sounds with playsound() at the same time is impossible since it is asynchronously.

    So what do I do!?!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sound in console
    By subflood in forum Linux Programming
    Replies: 2
    Last Post: 08-23-2004, 07:27 PM
  2. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 03:34 PM
  3. graphics in c/c++ console apps
    By anthonye in forum C Programming
    Replies: 2
    Last Post: 06-20-2002, 05:39 AM
  4. is it easy to add a gui to existing console apps
    By blight2c in forum Windows Programming
    Replies: 2
    Last Post: 05-18-2002, 11:15 PM
  5. Sound card interrupt problems
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 12-05-2001, 04:38 AM