I'm trying to create a program that randomly plays audio files on a continuous loop.

This is the code I currently have. Can someone help me solve this?

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
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <random>
#include <algorithm>

using namespace std;

int i;
int main() {
    string audio = "audio.wav";
    string audio2 = "audio2.wav";
    string audio3 = "audio3.wav";
    auto ints = vector<string> { "audio.wav", "audio2.wav", "audio3.wav" };

    auto rng = default_random_engine {};
    shuffle(ints.begin(), ints.end(), rng);

    for (auto i : ints) {
        PlaySound(ints[i], NULL, SND_SYNC);
    }
}
1. Get rid of line 12.

2. Put
1
2
3
4
5
6
7
8
auto rng = default_random_engine {};
while ( true ) {
    shuffle(ints.begin(), ints.end(), rng);

    for (auto i : ints) {
        PlaySound(ints[i], NULL, SND_SYNC);
    }
}
This is my updated code. It still gives an error when I build and run: : |23|error: no match for 'operator[]' (operand types are 'std::vector<std::__cxx11::basic_string<char> >' and 'std::__cxx11::basic_string<char>')|


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
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <random>
#include <algorithm>

using namespace std;

int main() {
    string audio = "audio.wav";
    string audio2 = "audio2.wav";
    string audio3 = "audio3.wav";
    auto ints = vector<string> { "audio.wav", "audio2.wav", "audio3.wav" };

auto rng = default_random_engine {};
while ( true ) {
    shuffle(ints.begin(), ints.end(), rng);

    for (auto i : ints) {
        PlaySound(ints[i], NULL, SND_SYNC);
    }
}
}
Line 23: PlaySound((LPCTSTR) i.c_str(), NULL, SND_SYNC);
Well if your PlaySound has the Unicode interface (which the standard for Windows for at least the last decade), then you need to convert your ANSI std::string into something the Win32 UNICODE API understands.

> Can someone help me solve this?
...
> It still gives an error when I build and run
Then WHY didn't you say this in your first post!?!?

Last edited on
Well if your PlaySound has the Unicode interface (which the standard for Windows for at least the last decade), then you need to convert your ANSI std::string into something the Win32 UNICODE API understands.


It's not my PlaySound. That function is from the Windows API. You didn't know that?
Furry Guy, that fixed it!
Topic archived. No new replies allowed.