PlaySound() sequence

Hi all

I have my code here :
1
2
3
4
5
6
7
int main(int argc, char* argv[])
{
PlaySound("C:\\Users\\Commander\\Desktop\\test.wav", NULL, SND_ASYNC);

        getch();
        return 0;
}


This much works fine.

My question is, how would change the code to play a short sequence of three wav files?
Perfect!

Thank you. :)
OK, I actually have an addition to this.

1
2
3
4
5
6
7
8
int main(int argc, char* argv[])
{
PlaySound("C:\\Users\\Commander\\Desktop\\test1.wav", NULL, SND_FILENAME);
PlaySound("C:\\Users\\Commander\\Desktop\\test2.wav", NULL, SND_FILENAME);
PlaySound("C:\\Users\\Commander\\Desktop\\test3.wav", NULL, SND_FILENAME);

        return 0;
}


I can play all three files just fine, but there is a short delay between that I would rather not be there.

I there a way to have no delay between the files playing?

I know playing the files this way is not what everyone would do, but before I try any of them can anyone tell me if it would be futile. ie Would I always have the 0.5 second gap between the files no matter what method I used?

And needless to say, are there any specific methods anyone would reccomend for this task?
Last edited on
maybe the reson for the pause is the file handling.
Try using SND_MEMORY and load the files into memory before playing
I was going to suggest loading the file into memory too. But I'm not sure that it truly fixes the problem. Here's my code which loads a wav file into memory, then plays it.

I find there is still a short gap between each sound.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <mmsystem.h>
#include <iostream>
#include <fstream>
#include <conio.h>

class Wave {

public:
    Wave(char * filename);
    ~Wave();
    void play(bool async=true);
    bool isok();

private:
    char * buffer;
    bool ok;
    HINSTANCE HInstance;
};

Wave::Wave(char * filename)
{
    ok = false;
    buffer = 0;
    HInstance = GetModuleHandle(0);

    ifstream infile(filename, ios::binary);

    if (!infile)
    {
         std::cout << "Wave::file error: "<< filename << std::endl;
        return;
    }

    infile.seekg (0, ios::end);   // get length of file
    int length = infile.tellg();
    buffer = new char[length];    // allocate memory
    infile.seekg (0, ios::beg);   // position to start of file
    infile.read (buffer,length);  // read entire file

    infile.close();
    ok = true;
}

Wave::~Wave()
{
    PlaySound(NULL, 0, 0); // STOP ANY PLAYING SOUND
    delete [] buffer;      // before deleting buffer.
}
void Wave::play(bool async)
{
    if (!ok)
        return;

    if (async)
        PlaySound(buffer, HInstance, SND_MEMORY | SND_ASYNC);
    else
        PlaySound(buffer, HInstance, SND_MEMORY);
}

bool Wave::isok()
{
    return ok;
}

int main(int argc, char *argv[]) {

    std::cout << "Trying to play sound ...\n";

    Wave one("sound1.WAV");
    Wave two("sound2.WAV");
    Wave three("sound3.WAV");

    one.play(0);
    two.play(0);
    three.play(0);

    std::cout << "press key to exit";
    getch();

    return 0;
}

At line 75-77 the sound is played, one after the other. If the parameter is omitted, like this,
75
76
77
    one.play();
    two.play();
    three.play();

then all three sounds will play simultaneously. Perhaps introducing a suitable duration Sleep() between each call will allow the sound to start playing at the required moment.
Last edited on
Thanks Chervil. Your code actually played for me with a much shorter pause between the files, although it did it in a weird way.

When I tried your code originally a few hours ago, the pauses were still there, but I tried it again now and it worked near perfectly. Also closed C++ builder and reopened it etc to make sure it wasn't a fluke.

The only thing I think I changed was freeing up some space on my computer from 18GB free to 26Gb free.

Last edited on
Topic archived. No new replies allowed.