Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I was trying to play songs in c++. I tried using the winmm lib and it worked fine. My goal now is to play multiple songs by creating some sort of song playlist and storing them in an array. I tried it but the playsound function keeps showing me errors. Can anyone advise me how i can do that?

**The error that i get is related to the text in the playsound function which shows
**LsongList is undefined**


C++
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
#include <mmsystem.h>
using namespace std;

int main()

{

  string songlist[3];
  songlist[0] = "a.wav";
  songlist[1] = "b.wav";
  songlist[2] = "c.wav";
  
for(int i =0; i<3; i++)
{
PlaySound(TEXT(songlist[i]), NULL, SND_ASYNC | SND_NODEFAULT );	



}

system("pause");



}
Posted
Comments
nv3 16-Nov-14 19:10pm    
The use of the TEXT macro is incorrect. Use string::c_str() instead. And if you are compiling with UNICODE option, you should use wstring instead of string.
Surajit Das 16-Nov-14 19:27pm    
@nv3:- Thanks for your reply. I tried this as per your advise-PlaySound(string::c_str(songlist[i]), NULL, SND_ASYNC | SND_NODEFAULT );
But it still shows me error- a nonstatic member object must be relative to a specific object. Can you kindly advise me how i can resolve this error
nv3 17-Nov-14 3:29am    
1. Use songlist[i]::c_str() as first argument.
2. You seem to be compiling with UNICODE option, so use wstring instead of string in the definition of songlist.
Surajit Das 17-Nov-14 4:39am    
@nv3:- Thanks for your help. I shall definitely try the way you said.

1 solution

I don't have speakers to test it now, but it compiles and runs without errors.


C++
void run()
{
	LPCWSTR songlist[3] = 
	{
		TEXT("C:\\sounds\\APPLAUSE.wav"),
		TEXT("C:\\sounds\\ARROW.wav"),
		TEXT("C:\\sounds\\BOMB.wav")
	};

	for(int i =0; i<3; i++)
	{
		PlaySound(songlist[i], NULL, SND_ASYNC | SND_NODEFAULT );	
	}

}


int _tmain(int argc, _TCHAR* argv[])
{
	run();
	system("pause");
	return 0;
}
 
Share this answer
 
Comments
Surajit Das 17-Nov-14 4:10am    
@BacchusBeale:-Thanks for your help. I tried the corrections you suggested me. Though it compiles correctly but there is one problem. When i run the program, it doesn't play the first two songs which i added in the array. It plays the last song only. Is there a way to fix it?
Richard MacCutchan 17-Nov-14 4:41am    
Do not use SND_ASYNC.
Surajit Das 17-Nov-14 7:58am    
@Richard MacCutchan:- Thanks Richard that fixed my error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900