如何让声音停止在OpenAL中播放

时间:2012-06-28 03:36:39

标签: c++ openal

我正在尝试在click事件上执行play play方法,然后在C ++中使用OpenAL in在调用版本上调用stop方法。我的问题是我无法让它停止播放。我播放声音的源代码如下:

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;
}`

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

1 个答案:

答案 0 :(得分:0)

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

第一个要播放的电话,sourceIndex将是1sourceIndex + 1)。下次你调用这个函数时(btw。的命名错误地命名为切换播放),那么sourceIndex将再次增加1,这将为你提供一个新的索引矢量。

相关问题