dimanche 15 mars 2015

Program frozen when playwave() started

I have encounter some problem when I build my .wav player using vs2013. When i load a wav it will play properly but whole program froze until the song ended. I could not even click StopButton.



void CALLBACK playSamples(HWAVEOUT hwo, UINT m, DWORD i, DWORD p1, DWORD p2)
{
static int last = ckIn.cksize / BUFFER_SIZE;
static int produced = 3;
static int consumed = 1;

if (m == WOM_DONE) {
if (produced < last) {
int which = produced%BUFFERS;
if (mmioRead(hmmioIn, (databuffer[which].lpData), BUFFER_SIZE) != BUFFER_SIZE) {
fprintf(stderr, "Error reading wave data\n");
mmioClose(hmmioIn, 0);
exit(-1);
}
waveOutWrite(hwo, &databuffer[which], sizeof(WAVEHDR));
produced++;
}

if (++consumed == last) {
finished = 1;
return;
}
}
}


and



void PlayWave(char *filename)
{
int i, err;
HWAVEOUT hAudioOut; // handle to the playback device

readWaveFile(filename);

// Open the audio playback device

err = waveOutOpen(&hAudioOut, WAVE_MAPPER, (WAVEFORMATEX *)&pcmWaveFormat, (DWORD)playSamples, 0L, CALLBACK_FUNCTION);

if (err) {

return;
}

// prepare the audio buffers

for (i = 0; i < BUFFERS; i++) {
databuffer[i].lpData = new char[BUFFER_SIZE];
databuffer[i].dwBufferLength = BUFFER_SIZE;
databuffer[i].dwFlags = 0;
memset(databuffer[i].lpData, 0, BUFFER_SIZE);
err = waveOutPrepareHeader(hAudioOut, &databuffer[i], sizeof(WAVEHDR));

if (err) {

return;
}
}

// fill the audio buffers

if (mmioRead(hmmioIn, (databuffer[0].lpData), BUFFER_SIZE) != BUFFER_SIZE) {
MessageBox::Show("Error reading wave data");
mmioClose(hmmioIn, 0);
return;
}
if (mmioRead(hmmioIn, (databuffer[1].lpData), BUFFER_SIZE) != BUFFER_SIZE) {
fprintf(stderr, "Error reading wave data");
mmioClose(hmmioIn, 0);
return;
}
if (mmioRead(hmmioIn, (databuffer[2].lpData), BUFFER_SIZE) != BUFFER_SIZE) {
fprintf(stderr, "Error reading wave data");
mmioClose(hmmioIn, 0);
return;
}

// Initially, play back 3 buffers of data

waveOutWrite(hAudioOut, &databuffer[0], sizeof(WAVEHDR));
waveOutWrite(hAudioOut, &databuffer[1], sizeof(WAVEHDR));
waveOutWrite(hAudioOut, &databuffer[2], sizeof(WAVEHDR));

while (!finished) { <- debug said music is being played here until end
if (g == 0){ // g = 0 when stop button is click

break;
}
}


// close mmio handle, and close the audio device

mmioClose(hmmioIn, 0);
for (i = 0; i < BUFFERS; i++) {
waveOutUnprepareHeader(hAudioOut, &databuffer[i], sizeof(WAVEHDR));
}
waveOutClose(hAudioOut);
g = 1;
return;
}


and function to trigger it



System::Void MyForm::button1_Click(System::Object^ sender, System::EventArgs^ e) {
array<Char>^sep = gcnew array<Char>{ ' ' };
if (listBox1->Items->Count != 0){
if (listBox1->SelectedIndex == -1)
MessageBox::Show("Select a song to play");
else{
String^ name = listBox1->SelectedItem->ToString();
array<String^>^result = name->Split(sep, StringSplitOptions::RemoveEmptyEntries);
//MessageBox::Show(result[0]);
char* pathname = (char*)(void*)Marshal::StringToHGlobalAnsi(result[0]);

PlayWave(pathname);
}

}
else
MessageBox::Show("There are no song in the list");


}

Aucun commentaire:

Enregistrer un commentaire