PlaySound in C++ Console application?

54,667

Solution 1

How can I play sounds without freezing the console?

If you Google for PlaySound this is the first result:

fdwSound

...

SND_ASYNC The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.

You should familiarise yourself with search engines and what they are capable of.

How do I add the wav file so it compiles as a release?

You need to link winmm.lib in both Release and Debug configurations. Alternatively, add

#pragma comment(lib, "winmm.lib")

to the top of your source code.

Solution 2

With mciSendstring() you can play multiple sounds simultaneously :)

Examples:

mciSendString("play wave1.wav", NULL, 0, NULL);

mciSendString("play wave2.wav", NULL, 0, NULL);

Solution 3

Most of your question were answered. However, for playing more sounds at once. For this purpose, you can do it by using SDL_Mixer. So far I was looking for any good way of playing audio, and making it work, I came to this.

The good point is, that SDL_Mixer is multiplatform (tested on both win, linux). If you know what you are doing, you can write code that compiles on both OSses.

Another good thing is, that you can play multiple sounds at once (maybe even up to 30, but check it online). The library is aviable for Windows (DLLs and header files) and Linux (installation through console). I will not go to further detail on instalation. It supports wav and oog and much more file types.

The drawbacks, however, are as follows: Upon making it work, wich took me hell lot of time, I figured out that audio crackles and is low quality, allthough the original file plays fine in media player. Upon research, I found out that this is for long time a bug in library, and still not fixed. Some people said they fixed it by putting volume up to 100 but it did not work for me. Second drawback is that it needs a bit more coding than just type

 PlaySound(TEXT("mywavsound.wav"), NULL, SND_FILENAME | SND_ASYNC);// - the correct code

Next drawback was figured by me, and that a wav file does not load, saying that the format is not correct. Allthough I used like 3 converters, and downloaded many files through internet, it did not work. It did work, however, upon downloading test project and using their .wav file in archive. However, I solved it by converting all my music to .oog

Like this, still I do not know what music library would be best to use. Depends what your console is supposed to be...? are you writing game or just media player? or just testing...?

Share:
54,667
aJynks
Author by

aJynks

Updated on January 05, 2022

Comments

  • aJynks
    aJynks over 2 years

    EDITED SO THE CODE IS CORRECT (THANKS TO ta.speot.is) - NEW QUESTION AT BOTTOM

    So I have been playing with the console as I am at that level, and we have been asked to make our first "project" for a assessment. I have the basic application all done.. but i wanted to jaz it up a bit and add some sounds. Sounds that will play from the console.

    This test works(kind of), as it will play a sound file, but there are 2 problems...

    1. When the sound starts playing, the application freezes until it finishes playing.

    2. When I try to compile as a "release" it errors with a "linking error" - fatal error LNK1120: 1 unresolved externals.

    #include <iostream>
    #include <windows.h>
    #include <mmsystem.h>
    using namespace std;
    
    int main(){
        //PlaySound(TEXT("mywavsound.wav"), NULL, SND_FILENAME); - My erroring code
        PlaySound(TEXT("mywavsound.wav"), NULL, SND_FILENAME | SND_ASYNC);// - the correct code
            
        int test = 0;
        cin>>test;
        return 0;
    }
    

    So my questions are...

    1. How can I play sounds without freezing the console, so I can for example play looping music file while the project is running? Also it would be great if I could play other sounds on top of it, for example when you press enter it will plays a sound without stopping the music.

    2. How do I add the wav file so it compiles as a release?

    EDIT

    I know about the SND_ASYNC thing but I do not know how to use it, I can't seem to use it without the thing not compiling.. does anyone have an example of a code using SND_ASYNC?

    EDIT 2

    So I have this working now.... using the

    PlaySound(TEXT("mysound.wav"), NULL, SND_FILENAME | SND_ASYNC);
    

    Now I am wondering how I can get 1 or more sounds to play at once, for if I call PlaySound() twice with that flag it will stop the 1st one and play the 2nd.. Is there a way to play 2 sounds at once?