1

I am writting a sound board. I would like it so when a person presses the button to play the sound, it would first see if a sound is stil playing. So it can then stop that sound and start the new one.

Is thier a way to see if a sound id still playing, I'm jsing the following code to start my sound

// Sound code
void StartSound(int id)
{
   AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    float actualVolume = (float) audioManager
            .getStreamVolume(AudioManager.STREAM_MUSIC);
    float maxVolume = (float) audioManager
            .getStreamMaxVolume(AudioManager.STREAM_MUSIC);

    float volume=(float) ((float)cGlobals.gVol/100.00);
    // Is the sound loaded already?

        soundPool.play(id, volume, volume, 1, 0, 1f);

}
StarPinkER
  • 14,081
  • 7
  • 55
  • 81
Ted pottel
  • 6,869
  • 21
  • 75
  • 134

2 Answers2

1

You can use isMusicActive() method of AudioManager.

0
class AudioSystem{
    /*
     * Checks whether the specified stream type is active.
     *
     * return true if any track playing on this stream is active.
     */
    public static native boolean isStreamActive(int stream, int inPastMs);
}

And for stream:

/** The audio stream for phone calls */
public static final int STREAM_VOICE_CALL = AudioSystem.STREAM_VOICE_CALL;
/** The audio stream for system sounds */
public static final int STREAM_SYSTEM = AudioSystem.STREAM_SYSTEM;
/** The audio stream for the phone ring */
public static final int STREAM_RING = AudioSystem.STREAM_RING;
/** The audio stream for music playback */
public static final int STREAM_MUSIC = AudioSystem.STREAM_MUSIC;
/** The audio stream for alarms */
public static final int STREAM_ALARM = AudioSystem.STREAM_ALARM;
/** The audio stream for notifications */
public static final int STREAM_NOTIFICATION = AudioSystem.STREAM_NOTIFICATION;
/** @hide The audio stream for phone calls when connected to bluetooth */
public static final int STREAM_BLUETOOTH_SCO = AudioSystem.STREAM_BLUETOOTH_SCO;
/** @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */
public static final int STREAM_SYSTEM_ENFORCED = AudioSystem.STREAM_SYSTEM_ENFORCED;
/** The audio stream for DTMF Tones */
public static final int STREAM_DTMF = AudioSystem.STREAM_DTMF;
/** @hide The audio stream for text to speech (TTS) */
public static final int STREAM_TTS = AudioSystem.STREAM_TTS;

Check for whatever type of audio stream you want to check. For example, AudioSystem.isStreamActive(STREAM_MUSIC, 0) to check whether any music is active.

StarPinkER
  • 14,081
  • 7
  • 55
  • 81
  • 1
    Android class `AudioSystem` is now replaced with [`AudioManager`](http://developer.android.com/reference/android/media/AudioManager.html) class and seems like there isn't such method like `isStreamActive()`. – Micer Nov 03 '13 at 17:11
  • Not working :( this method is not available any more. – arslan haktic Mar 30 '15 at 11:52