2012-06-28 59 views
1

我試圖在Click事件上執行播放聲音方法,然後在C++中使用OpenAL在發行版上調用停止方法。我的問題是,我無法讓它停止播放發佈。我的源代碼來播放聲音如下:如何讓聲音在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; 
}` 

我認爲問題是,當它被稱爲第二次,當它應該被停止,這是一個不同的來源,這就是爲什麼它的狀態不在播放。如果這是問題,那麼我如何訪問相同的源代碼?

回答

0

當然,它與以前不一樣,你增加每個呼叫的sourceIndex變量。

所以打第一個電話,sourceIndex將是1sourceIndex + 1)。當你下次調用這個函數時(這個函數會被切換爲),那麼sourceIndex會再次增加1,這會給你一個新的源矢量索引。

+0

謝謝是的,我嘗試了一個不變的來源以及停止所有來源與alsourcestopv,但似乎沒有工作。 –