Attempting to read audio file

closed account (1Afj1hU5)
UPDATE: The title of this topic has changed. I have managed to fix the first problem I was having, only to be greeted by another. I will copy/paste the current problem here to save you from having to go through the dialogues below:

1
2
3
4
5
6
7
void playAudio(char* audioFile)
{
	mciSendString(L"open " + audioFile + " type waveaudio alias song", NULL, 0, 0); // Doesn't work, cannot add const char* to another char*
	mciSendString(L"play song wait", NULL, 0, 0); 
	mciSendString(L"close song", NULL, 0, 0);
}

...And yeah, that's pretty much the problem. Quite simple, just trying to concatenate things together to form a full path.


OLD: Hello, so I am trying to build a multi-threaded audio player, but first I need to figure out why mciSendString not playing any of my files. Below is how I'm using it, and according to MSDN that's how you should be using it. Where am I going wrong?

1
2
3
4
5
6
7
8
9
int main()
{
	mciSendString(L"open C:\\Users\\Jack Shields\\Desktop\\cat_y.wav", NULL, 0, 0);
	mciSendString(L"play C:\\Users\\Jack Shields\\Desktop\\cat_y.wav", NULL, 0, 0);
 
	int iTemp = 0;
	cin >> iTemp;
	return 0;
}


Thanks.
Last edited on
Why don't you check the return value and see what's wrong?
https://msdn.microsoft.com/de-de/library/windows/desktop/dd757161.aspx
closed account (1Afj1hU5)
Hey Thomas, I did that, thank you so much. I was able to find out what the problem was. Turns out I needed to specify the type of the file, in my case it's a waveaudio file. Now that I got it to work with a hard-coded file, I'm having another issue. So basically I want the user to be able to select as many audio files they want and I want all of them to play concurrently. The multi-threading part is easy, but squeezing in the file name inside the mciSendString function I'm finding a bit challenging. Here's an example of what I'm having trouble with:
1
2
3
4
5
        char* audio = C:\\Test\\bird.wav;
 
        mciSendString(L"open " + audio + " type waveaudio alias song", NULL, 0, 0);
	mciSendString(L"play song wait", NULL, 0, 0); 
	mciSendString(L"close song", NULL, 0, 0);


It's not reading the " + audio + " part.
closed account (1Afj1hU5)
Still stuck, I'm open for any suggestions.
A char* doesn't work with the + operator.
Try this:
1
2
string audio = "open " + "C:\\Test\\bird.wav";
mciSendString(audio.c_str(), " type waveaudio alias song", NULL, 0, 0);


BTW for .wav file you can use PlaySound which is easier to use.
https://msdn.microsoft.com/en-us/library/windows/desktop/dd743680(v=vs.85).aspx
closed account (1Afj1hU5)
Alright, I'll give this piece of code a shot. Also I agree, but I need my application to be multi-threaded and I do not think PlaySound is eligible for multi-threaded behavior.
It has a parameter SND_ASYNC which plays it asynchronously.
Do you plan to make a GUI ?
closed account (1Afj1hU5)
Err... This doesn't seem to work as mciSendString only accept 4 parameters and not 5. Also for some reason in the string audio = "open " + "C:\\Test\\bird.wav"; when I hover my cursor on the C:\\Test\\bird.wav it reads "expression must have integral or unscoped enum type" this is driving me crazy... Even the audioc_str() reads "argument of type "const char*" is incompatible with parameter of type "LPCWSTR".
closed account (1Afj1hU5)
Can you give me an example of two or more PlaySound functions firing at the same time? Because this was my earlier attempt and it didn't seem to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void playSecondSound()
{
	PlaySoundA("C:\\Test\\bird.wav", NULL, SND_ASYNC); 
}

int main()
{
	thread worker1(playSecondSound);
	PlaySoundA("C:\\Test\\cat_y.wav", NULL, SND_ASYNC);

	worker1.join();

	int iTemp = 0;
	cin >> iTemp;
	return 0;
}
closed account (1Afj1hU5)
I have updated my question.
closed account (1Afj1hU5)
I fixed my own problem. For those who are (or will) encounter this issue in the future, here's what I did:
1
2
3
4
5
6
7
8
9
10
11
12
13
void playAudio(char* audioFile)
{
    char theFile[MAX_PATH + 64] = { 0 };
    sprintf(theFile, "open %s Type waveaudio alias song", audioFile);

    sendCommand(theFile);
    sendCommand("play song wait");
    sendCommand("close song");
}
void sendCommand(char* file)
{
    mciSendStringA(file, NULL, 0, 0);
}


Took quite some thinking but I'm happy it's working now. If you do not intend to have your program play two audio files at once, you're better off using the PlaySound() function instead. I decided to use the mciSendStringA() function for multi-threaded behavior.

Thank you.
Last edited on
Topic archived. No new replies allowed.