PaulsCode Forum

General => Other Support => Topic started by: eldritchsandwich on June 03, 2013, 11:59:50 AM

Title: Stop/Start BG Music During Another Sound
Post by: eldritchsandwich on June 03, 2013, 11:59:50 AM
Hi Paul,

I've got an application that plays some looping music, but I'd like the looping music to Pause temporarily while a certain sound plays and then pick back up where it left off.

I've managed to get this to partially work by stopping the looping source, starting the new sound, and then queuing the looping source to play again on that new sound source. This causes the looping track to start over from the beginning, which I can live with. However, if I then call this method again it appears I'm no longer tracking the same looping source, so I can't stop it anymore. Further, if I Pause and then Unpause my game, the original looping track starts to play over everything.

I'm doing something like this, but when I try to call this method a second time, after my loopingTrackSource has been queued up, it's like loopingTrackSource is no longer what's playing that sound so I can't stop it again:

mySoundSystem.stop(loopingTrackSource);            
mySoundSystem.play(newSoundSource);
controller.mySoundSystem.queueSound(newSoundSource, loopingTrackURL, loopingTrackSource);


Is there a way to track what source(s) is/are currently playing, or maybe a way to reference what has been queued, or something along those lines? What I'm trying to accomplish is to have some background music playing that will pause when a certain other sound plays and then pick up where it left off.

Does anyone have any thoughts or advice?
Title: Re: Stop/Start BG Music During Another Sound
Post by: Paul on June 03, 2013, 02:38:38 PM
I would use the IStreamListener to know when the second sound has finished playing.


First, have your class implement IStreamListener, and add a string to hold the second source name:
Code: [Select]
Class MyClass implements IStreamListener
{
    String sourceB = null;


Set up your first streaming source and begin looping it:
Code: [Select]
    mySoundSystem.newStreamingSource( true, "Source A", urlA, identifierA, true, 0, 0, 0, SoundSystemConfig.ATTENUATION_NONE, 0 );
    mySoundSystem.play( "Source A" );


Some event happens... quick-stream the second streaming source, and pause the first one:
Code: [Select]
    sourceB = mySoundSystem.quickStream( true, urlB, identifierB, false, 0, 0, 0, SoundSystemConfig.ATTENUATION_NONE, 0 );
    mySoundSystem.pause( "Source A" );


And finally, implement the IStreamListener interface to restart the first streaming source when the second one finishes:
Code: [Select]
    public void endOfStream( String sourcename, int queueSize )
    {
        if( sourceB != null && sourceB.equals( sourcename ) )
        {
            mySoundSystem.play( "Source A" );
            sourceB = null;
        }
    }


Of course, you'll probably want to use something a little more thread-safe when handling that temporary source name, but you get the basic idea.
Title: Re: Stop/Start BG Music During Another Sound
Post by: eldritchsandwich on June 04, 2013, 02:57:04 PM
This is great, I wasn't aware of the IStreamListener interface. It's exactly what I'm looking for. Thanks for the quick response!