Now that we have the basic functionality for playing sounds implemented using the MySound
and MyChannel
classes, we can begin adding more features to it. We will start with the simplest of all, pausing the sound.
We must add a member variable to hold the pause state, and some methods to modify it. We must also remember to initialize this value to false
inside the constructor, and inside the Play()
method:
public: bool GetPaused() const { return paused; } void SetPaused(bool value) { paused = value } private: bool paused;
Next, all we have to do is add a very simple condition at the beginning of the WriteSoundData()
method so that it does nothing when the sound is paused. That is as simple as it gets!
void MyChannel::WriteSoundData(PCM16* data, int count) { if(sound == 0 || paused) return; for (int i = 0; i < count; i += 2) { if(position >= sound->count) { Stop(); return; } PCM16 value = sound->data[position]; data[i] = value;...