Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 224
Registered: ‎03-29-2012
My Device: 9900
My Carrier: Wind

alSourceStop not working

I can get a sound to play on touch but when it is released, it does not stop as I am trying to accomplish. The code for starting the sound calls this function: 

 

bool SoundManager::play(QString fileName, float pitch, float gain)
{
    static uint sourceIndex = 0;

    // 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;
            }
            // Play the source.
            alSourcePlay(source);
            
            if (alGetError() != AL_NO_ERROR) {
                reportOpenALError();
                return false;
            }
        }
    } else {
        // The buffer was not found.
        return false;
    }

 

and then to stop it, I try stopping the sources: 

 

bool SoundManager::stop(QString fileName, float pitch, float gain)
{
	static uint sourceIndex = 0;

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

	//NOTE DANNNNNNN HERREEEE try using the state, if Al_PLAYING==TRUE, stop, else PLAY
	            // Play the source.
	            alSourceStopv(SOUNDMANAGER_MAX_NBR_OF_SOURCES-1, mSoundSources);
	            
	            if (alGetError() != AL_NO_ERROR) {
	                reportOpenALError();
	                return false;
	            }
	        }
	    } else {
	        // The buffer was not found.
	        return false;
	    }

	    return true;


}

 it is crude but I am new to OpenAL; currently the sound does not stop using this stop function, even though I try and stop all sources just to be sure.

Developer
Posts: 224
Registered: ‎03-29-2012
My Device: 9900
My Carrier: Wind

Re: alSourceStop not working

[ Edited ]
bool SoundManager::play(QString fileName, float pitch, float gain)
{
    uint sourceIndex = 0; //static means it will only assign 0 once, then it will be one next time
    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) % SOUNDMANAGER_MAX_NBR_OF_SOURCES;
        // Get the source in which the sound will be played.
       static 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_STOPPED){
            alSourcePlay(source);

            }
            else if (state==AL_PLAYING){

            	alSourceStop(source);
            }
            else {
            	reportOpenALError();
            }

            if (alGetError() != AL_NO_ERROR) {
                reportOpenALError();
                return false;
            }
        }
    } else {
        // The buffer was not found.
        return false;
    }

    return true;
}

 

I have also tried just making it call the play again and play the sound if AL_SOURCE_STATE is stopped or initial, and stop it if the state is AL_SOURCE_STATE_STOPPED but this has no effect in stopping it. The only time it works is if i put the stop command right after a play command but this is not helpful.