我正在尝试制作一个可以同时播放7种声音的c ++程序。为此,我有7个由Qt Designer 5.3.1组成的播放按钮,每当我点击一个按钮时,它就会将声音加载到相应的缓冲区。我正在使用OpenAL播放声音。检查声音文件后,如果是wav文件,我使用以下代码将数据加载到缓冲区:
//now we put our data into the openAL buffer and
//check for success
if (ui->groupBox_2->isHidden() == true) //play button is in the groupbox, so I'm using groupbox.
{
alGetError();
alBufferData(buffer[0], *format, data, *size, *freq);
}
else if (ui->groupBox_5->isHidden() == true)
{
alGetError();
alBufferData(buffer[1], *format, data, *size, *freq);
}
else if (ui->groupBox_14->isHidden() == true)
{
alGetError();
alBufferData(buffer[2], *format, data, *size, *freq);
}
else if (ui->groupBox_23->isHidden() == true)
{
alGetError();
alBufferData(buffer[3], *format, data, *size, *freq);
}
else if (ui->groupBox_26->isHidden() == true)
{
alGetError();
alBufferData(buffer[4], *format, data, *size, *freq);
}
else if (ui->groupBox_29->isHidden() == true)
{
alGetError();
alBufferData(buffer[5], *format, data, *size, *freq);
}
else if (ui->groupBox_32->isHidden() == true)
{
alGetError();
alBufferData(buffer[6], *format, data, *size, *freq);
}
if (alGetError() != AL_NO_ERROR)
{
return false;
}
播放声音:
alGenSources(7, source);
// Attach the buffer to an available source.
QObject* button = QObject::sender();
if (button == ui->pushButton_2)
{
alSourcei(source[0], AL_BUFFER, buffer[0]);
alGetSourcei(source[0], AL_SOURCE_STATE, &state);
if (state != AL_PLAYING)
{
alSourcePlay(source[0]);
alGetSourcei(source[0], AL_SOURCE_STATE, &state);
if (state == AL_PLAYING)
{
ui->groupBox_2->setTitle(name[0] + " is Playing...");
}
}
if (alGetError() != AL_NO_ERROR)
{
return false;
}
}
else if (button == ui->pushButton_35)
{
alSourcei(source[1], AL_BUFFER, buffer[1]);
alGetSourcei(source[1], AL_SOURCE_STATE, &state);
if (state != AL_PLAYING)
{
alSourcePlay(source[1]);
alGetSourcei(source[1], AL_SOURCE_STATE, &state);
if (state == AL_PLAYING)
{
ui->groupBox_5->setTitle(name[1] + " is Playing...");
}
}
if (alGetError() != AL_NO_ERROR)
{
return false;
}
}
else if (button == ui->pushButton_39)
{
alSourcei(source[2], AL_BUFFER, buffer[2]);
alGetSourcei(source[2], AL_SOURCE_STATE, &state);
if (state != AL_PLAYING)
{
alSourcePlay(source[2]);
alGetSourcei(source[2], AL_SOURCE_STATE, &state);
if (state == AL_PLAYING)
{
ui->groupBox_14->setTitle(name[2] + " is Playing...");
}
}
if (alGetError() != AL_NO_ERROR)
{
return false;
}
}
else if (button == ui->pushButton_43)
{
alSourcei(source[3], AL_BUFFER, buffer[3]);
alGetSourcei(source[3], AL_SOURCE_STATE, &state);
if (state != AL_PLAYING)
{
alSourcePlay(source[3]);
alGetSourcei(source[3], AL_SOURCE_STATE, &state);
if (state == AL_PLAYING)
{
ui->groupBox_23->setTitle(name[3] + " is Playing...");
}
}
if (alGetError() != AL_NO_ERROR)
{
return false;
}
}
else if (button == ui->pushButton_47)
{
alSourcei(source[4], AL_BUFFER, buffer[4]);
alGetSourcei(source[4], AL_SOURCE_STATE, &state);
if (state != AL_PLAYING)
{
alSourcePlay(source[4]);
alGetSourcei(source[4], AL_SOURCE_STATE, &state);
if (state == AL_PLAYING)
{
ui->groupBox_26->setTitle(name[4] + " is Playing...");
}
}
if (alGetError() != AL_NO_ERROR)
{
return false;
}
}
else if (button == ui->pushButton_51)
{
alSourcei(source[5], AL_BUFFER, buffer[5]);
alGetSourcei(source[5], AL_SOURCE_STATE, &state);
if (state != AL_PLAYING)
{
alSourcePlay(source[5]);
alGetSourcei(source[5], AL_SOURCE_STATE, &state);
if (state == AL_PLAYING)
{
ui->groupBox_29->setTitle(name[5] + " is Playing...");
}
}
if (alGetError() != AL_NO_ERROR)
{
return false;
}
}
else if (button == ui->pushButton_55)
{
alSourcei(source[6], AL_BUFFER, buffer[6]);
alGetSourcei(source[6], AL_SOURCE_STATE, &state);
if (state != AL_PLAYING)
{
alSourcePlay(source[6]);
alGetSourcei(source[6], AL_SOURCE_STATE, &state);
if (state == AL_PLAYING)
{
ui->groupBox_32->setTitle(name[6] + " is Playing...");
}
}
if (alGetError() != AL_NO_ERROR)
{
return false;
}
}
但是当我执行此代码时,只播放最后加载的声音。当我从“alBufferData”函数中删除if和elses时,最后加载的声音同时播放7次。那么,在这种情况下如何同时播放7种不同的声音?