Author Topic: Stop/Start BG Music During Another Sound  (Read 2312 times)

Offline eldritchsandwich

  • bit
  • Posts: 2
    • View Profile
Stop/Start BG Music During Another Sound
« 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?

Offline Paul

  • Administrator
  • double
  • *****
  • Posts: 3496
  • Developer
    • View Profile
    • PaulsCode.Com
Re: Stop/Start BG Music During Another Sound
« Reply #1 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.
« Last Edit: June 03, 2013, 02:41:05 PM by Paul »
Device: Samsung Galaxy Nexus i515
CPU: TI OMAP4460, 1.2 GHz (dual core, ARM Cortex-A9)
GPU: PowerVR SGX540, 307 MHz
RAM: 1 GB
Resolution: 720 x 1280
Rom: omni-4.4.4-20141014-toro-FML KitKat 4.4.4, rooted

Device: Eee PC 1015PEM
CPU: Intel Atom N550, 1.5 GHz (dual core, x86)
GPU: Intel GMA 3150, 200 MHz (dual core)
RAM: 2GB
Resolution: 1024 x 600
Rom: android-x86-4.3-20130725 Jelly Bean 4.3, rooted

Offline eldritchsandwich

  • bit
  • Posts: 2
    • View Profile
Re: Stop/Start BG Music During Another Sound
« Reply #2 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!