我试图在单击事件上执行播放声音方法,然后在发布时调用停止方法,在 C++ 中使用 OpenAL。我的问题是我无法让它在发布时停止播放。我播放声音的源代码如下:
bool SoundManager::play(QString fileName, float pitch, float gain)
{
static uint sourceIndex = 0;
ALint state;
// Get the corresponding buffer id set up in the init function.
ALuint bufferID = mSoundBuffers[fileName];
if (bufferID != 0) {
// Increment which source we are using, so that we play in a "free" source.
sourceIndex = (sourceIndex + 1) % SOUNDMANAGER_MAX_NBR_OF_SOURCES;
// Get the source in which the sound will be played.
ALuint source = mSoundSources[sourceIndex];
if (alIsSource (source) == AL_TRUE) {
// Attach the buffer to an available source.
alSourcei(source, AL_BUFFER, bufferID);
if (alGetError() != AL_NO_ERROR) {
reportOpenALError();
return false;
}
// Set the source pitch value.
alSourcef(source, AL_PITCH, pitch);
if (alGetError() != AL_NO_ERROR) {
reportOpenALError();
return false;
}
// Set the source gain value.
alSourcef(source, AL_GAIN, gain);
if (alGetError() != AL_NO_ERROR) {
reportOpenALError();
return false;
}
alGetSourcei(source, AL_SOURCE_STATE, &state);
if (state!=AL_PLAYING)
alSourcePlay(source);
else if(state==AL_PLAYING)
alSourceStop(source);
if (alGetError() != AL_NO_ERROR) {
reportOpenALError();
return false;
}
}
} else {
// The buffer was not found.
return false;
}`
我认为问题在于第二次调用它时,应该停止它,它是一个不同的源,这就是它的状态不播放的原因。如果这是问题,那么我如何访问相同的源?