-1

In my app I am playing sound if user press on layout. But the problem is if user press layout again and again. Sound repeating again and again. I want layout become disable if sound is playing and enable when sound finish. so that user able to play sound again when its finish.

Code-

public class Boy1 extends Activity implements OnTouchListener {

    private SoundPool soundPool;
    private int soundID;
    boolean loaded = false;
    private LinearLayout layout1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.boy);
        layout1 = (LinearLayout) findViewById (R.id.layout1);
        layout1.setOnTouchListener(this);
        // Set the hardware buttons to control the music
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        // Load the sound
        soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId,
                    int status) {
                loaded = true;
            }
        });
        soundID = soundPool.load(this, R.raw.test, 1);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // Getting the user sound settings
            AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
            float actualVolume = (float) audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC);
            float maxVolume = (float) audioManager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            float volume = actualVolume / maxVolume;
            // Is the sound loaded already?
            if (loaded) {
                soundPool.play(soundID, volume, volume, 1, 0, 1f);
                Log.e("Test", "Played sound");
            }
        }
        return false;
    }
John R
  • 2,078
  • 8
  • 35
  • 58

1 Answers1

0

Maintain an boolean for sound is playing or not...and based on that disable or enable your Layout as follows...

if (isSoundPlaying) {
    layout1.setEnabled(false);
} else {
    layout1.setEnabled(true);
}

Update:

when sound start playing then write layout1.setEnabled(false); it will disable the layout. as I understand soundPool.play(soundID, volume, volume, 1, 0, 1f); start the sound laying then write...

soundPool.play(soundID, volume, volume, 1, 0, 1f);
layout1.setEnabled(false);

And when sound playing stops then write which will enable the layout...

layout1.setEnabled(true);
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41