如何在OpenAL中获得停止播放的声音 我试图在单击事件中执行播放声音方法,然后在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; }`

如何在OpenAL中获得停止播放的声音 我试图在单击事件中执行播放声音方法,然后在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; }`,c++,openal,C++,Openal,我认为问题在于,当它被第二次调用时,当它应该被停止时,它是一个不同的源,这就是为什么它的状态不起作用。如果这是一个问题,那么我如何访问相同的源呢?当然,它与以前的源不一样,每次调用都要增加sourceIndex变量 因此,要播放的第一个调用,sourceIndex将是1(sourceIndex+1)。下次调用该函数时(顺便说一句,该函数因切换播放而命名不好),那么sourceIndex将再次增加1,这将为源向量提供一个新的索引。谢谢,是的,我尝试了一个不变的源,并使用alsourcestopv停

我认为问题在于,当它被第二次调用时,当它应该被停止时,它是一个不同的源,这就是为什么它的状态不起作用。如果这是一个问题,那么我如何访问相同的源呢?

当然,它与以前的源不一样,每次调用都要增加
sourceIndex
变量


因此,要播放的第一个调用,
sourceIndex
将是
1
sourceIndex+1
)。下次调用该函数时(顺便说一句,该函数因切换播放而命名不好),那么
sourceIndex
将再次增加1,这将为源向量提供一个新的索引。

谢谢,是的,我尝试了一个不变的源,并使用alsourcestopv停止了所有源,但似乎都不起作用。