Need to play .wav files in a for loop, one by one, like a playlist

I want to play a list of .wav files, stored in a queue automatically(using Dirent.h) using the path entered by the user. Everything works well but the for oop soes not pause or wait for the sound to be played, instead, it only plays the last file. I need to play all files, any help would be appreciated. P.S: I know about SND_SYNC but I want the program to stay responsive all the time, so I can stop, prev, next, replay, etc while the playlist is being played, This is my whole code:
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include<iostream>
#include<conio.h>
#include<string>
#include<cstring>
#include<dirent.h>
#include<random>
#include<windows.h>
#pragma comment (lib, "winmm.lib")

using namespace std;

class playlist{
private:
	string Q[100];
	DIR *dir;
	struct dirent *ent;
	int s, r1, ra, f, r, t;
	char ch;
public:
	playlist(){
		for(int i=0; i<100; i++)
			Q[i] = "Empty";
		s =  r1 = t = 0;
		ra = 1;
		ch = '\0';
		f = r = -1;
	}
	void set_folder(){
		//input folder to scan for files from user and put them in queue
		string path[100];
		for(int i=0; i<100; i++)
			path[i] = "Empty";
		int i=0;
		string p;
		cout<<"\n\n\tPlz, enter path for music files :: ";
		getline(cin, p);
		char *temp = new char[100];
		strcpy(temp, p.c_str());
		if ((dir = opendir (temp)) != NULL) {
		  while ((ent = readdir (dir)) != NULL) {
		    path[i] = ent->d_name;
			i++;
		  }
		  for(int i=0; i<100 && path[i]!="Empty"; i++){
			  Q[i] = p + "\\" + path[i];
			  cout<<Q[i]<<endl;
		  }
		  closedir (dir);
		  delete[] temp;
		int a = 0;
		for(int i=0; i<100; i++)
			if(Q[i] != "Empty")
				a++;
		r = a;
	}
	}
	void check(){		
		if(ra == 1)
			if(f == r)
				f = -1;
		if(r1 == 1)
			f--;
	}
	void play(){
		if(s == 1)
			while(s == 1)
				shuffle();
		else
			for(int i=f; i<=r; i++){
				LPSTR s = const_cast<char *>(Q[f].c_str());
				PlaySound(TEXT (s), NULL, SND_ASYNC | SND_NODEFAULT );
				f++;
			}
	}
	void start(){
		f = 0;
		play();
	}
	void next(){
		//skip current song and play next in queue/playlist, if shuffle=1, call shuffle
		f++;
		play();
	}
	void prev(){
		//skip current song and play prev in queue/playlist, if shuffle=1, call shuffle
		f--;
		play();
	}
	void stop(){
		//stop playing the current song and reset the playlist
		PlaySound(NULL, 0, 0);
		f= -1;
	}
	void rep(){
		//stop playing and start playing again, call stop then start
		stop();
		start();
	}
	void view_op(){
		cout<<"Curently...\n\n\n";
		if(s==1)
			cout<<"\tShuffle is on!";
		else
			cout<<"\tShuffle is off!";
		if(r1==1){
			cout<<"\n\tRepeating current song!";
			ra = 0;
		}
		else if(ra==1){
			cout<<"\n\tRepeating all songs!";
			r1 = 0;
		}
	}
	void set_op(){
		cout<<"\n\n";
		cout<<"\n\tWhat would you like to do?\n";
		cout<<"\n\t1) Toggle Shuffle ON / OFF";
		cout<<"\n\t2) Toggle Repeat ONE / ALL";
		cout<<"\n\t3) Nothing\n\n\t";
		ch = getche();
		switch(ch){
		case '1':
			if(s==1)
				s = 0;
			else
				s = 1;
			break;
		case '2':
			if(r1==1){
				ra = 1;
				r1 = 0;
			}
			else{
				ra = 0;
				r1 = 1;
			}
			break;
		}
	}
	void opt(){
		system("color 06");
		do{
			system("cls");
			view_op();
			set_op();
			system("cls");
			view_op();
		}
		while(ch != '3');
	}
	void shuffle(){
		//play any random NON-EMPTY index of the queue
		//shuffle for an integer eqalent to the non-empty indexes in queue
		//f = index shuffled to
		//after played, shuffle again
		random_device dev;
		uniform_int_distribution<> random(2,r);
		f=random(dev);
		LPSTR s = const_cast<char *>(Q[f].c_str());
		PlaySound(TEXT(s), NULL, SND_SYNC | SND_NODEFAULT );
	}
	void display(){
		if(t==0){
			system("cls");
			set_folder();
			t++;
		}
		system("cls");
		system("color 0D");
		cout<<"\n\tM         M          PPPPPPPPP         333333333";
		cout<<"\n\tM M     M M          P        P              33";
		cout<<"\n\tM  M   M  M          P        P            33";
		cout<<"\n\tM   M M   M          PPPPPPPPP          3333333          P L A Y E R";
		cout<<"\n\tM    M    M          P                         3";
		cout<<"\n\tM         M          P                         3";
		cout<<"\n\tM         M          P                 33333333";
		cout<<"\n\n\n\n\t\t1) >	Start Playlist";
		cout<<"\n\t\t2) >>	Skip/Next";
		cout<<"\n\t\t3) <<	Previous";
		cout<<"\n\t\t4) #	Stop";
		cout<<"\n\t\t5) <-'	Replay Playlist";
		cout<<"\n\t\t6) ...	Playlist Options";
		cout<<"\n\t\t7) ^	Reset Folder";
		cout<<"\n\t\t8) X	Exit\n\n\t";
		ch = getche();
		switch(ch){
		case '1':
			start();
			break;
		case '2':
			next();
			break;
		case '3':
			prev();
			break;
		case '4':
			stop();
			break;
		case '5':
			rep();
			break;
		case '6':
			opt();
			break;
		case '7':
			t=0;
			break;
		case '8':
			exit(1);
		default:
			cout<<"\n\n\tWrong input!!!";
			break;
		}
	}
	void loading(){
		system("color 0B");
		cout<<"\n\n\n\tLOADING: ";
		for (int i=0; i<55; i++){
			Sleep ( 50 );
			cout<<"|";
		}
	}
	void welcome(){
		PlaySound(TEXT("E:\\Mp3 files\\taa_daa.wav"), NULL, SND_ASYNC );
		cout<<"\n\n\n\n\n\t\twwwww            wwwww"<<endl;
		cout<<"\t\t www       w      www"<<endl;
		cout<<"\t\t  www     www    www"<<endl;
		cout<<"\t\t   www   wwwww  www"<<endl;
		cout<<"\t\t    wwwwwwwwwwwwww"<<endl;
		cout<<"\t\t     wwww    wwww e l c o m e ! ! !"<<endl;
		cout<<"\n\tPress any key to continue...";
		int a = 150;
		system ( "color 01" );
		Sleep ( a );		
		system ( "color 02" );
		Sleep ( a );		
		system ( "color 03" );
		Sleep ( a );		
		system ( "color 04" );
		Sleep ( a );		
		system ( "color 05" );
		Sleep ( a );		
		system ( "color 06" );
		Sleep ( a );		
		system ( "color 07" );
		Sleep ( a );		
		system ( "color 08" );
		Sleep ( a );		
		system ( "color 09" );
		Sleep ( a );		
		system( "color 0A" );
		Sleep( a );		
		system( "color 0B" );
		Sleep( a );		
		system( "color 0C" );
		Sleep( a );		
		system( "color 0D" );
		Sleep( a );		
		system( "color 0E" );
		Sleep( a );		
		system( "color 0F" );
		Sleep( a );
		system( "color 03" );
			getche();
	}
	void Mp3Player(){
		welcome();
		loading();
		do{
			display();
		}
		while(1);
	}
};

void main (){
	playlist X;
	X.Mp3Player();
}
I dont know much about winmm.lib which seems part of the Windows SDK... i used DirectSound in the past (from Directx SDK) and going to use it in the next project. DirectSound is more powerfull than winmm.lib.

however... the solution you are looking for is by creating a new thread that handles playing of the sounds, thus, your GUI will stay responsive even if you used SND_SYNC... I guess.

I cant actually help you further, since I never used winmm.lib or threading. either wait for someone with better knowledge or read about the topic.
PlaySound is not designed for heavy duty (or even really medium duty) usage. It's really just a quick-n-easy way to play a single sound file. As soon as you start doing more complex things, it becomes extremely clunky.

I recommend you get an audio lib that is better at this kind of thing. Either SFML or BASS would make this trivial.


I'm not even sure it's possible to do what you want with PlaySound. I can conceive of ways to have stop/next/etc controls... but those plus detecting when a song is done and moving onto the next one? That stumps me.

CppVoidppC's idea of spawning a separate thread and using SND_SYNC will work for playing consecutive tunes... but will not work if you want to stop a tune already playing.

Likewise, spawning a separate thread and using SND_ASYNC will work for stopping a tune already playing, but will not work for playing consecutive tunes.




So yeah... get BASS or SFML or something. Though I don't think SMFL supports mp3. BASS should.




EDIT:

Unless you can stop a playing SND_SYNC tune by playing another one in a different thread.

IE:
- subthread plays 'longfile.mp3'
- main thread wants to stop it to main thread plays 'short_and_silent.mp3' while subthread is still playing

If that stops subthread, then you can have all of your controls + playlist functionality.

But talk about hacky.

Last edited on
Topic archived. No new replies allowed.