0

I am having trouble figuring out how to manage a togglebutton that turns music on/off. When a user clicks it on, it should just loop continuously until it is turned off. I am using soundpool, if there was someway i could detect if my media was playing either by using soundpool or media player that would help. But i cant find anything relevant in documentation and similar question doesnt have a good answer; How to tell if a sound is still playing in soundpool

I have this so far:

final ToggleButton tglbuttn = (ToggleButton) findViewById(R.id.toggleButton1);

        final SoundPool mySp = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
        final int soundId = mySp.load(this, R.raw.music, 1);

        tglbuttn.setChecked(????????); //what could go here?
        tglbuttn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {


            }

If i could somehow get something like this bluetooth toggleswitch going, but with soundpool/mediaplayer, a way to tie in the actual status of media playing with the state of the togglebutton:

tglbtn.setChecked(myBluetooth.isEnabled());
Community
  • 1
  • 1

1 Answers1

2

You can use isChecked method to toggle the button. Try this:

//Make your activity implements OnClickListener
public class MyActivity extends Activity implements OnClickListener {

Then:

//In your OnCreate()
tglbuttn.setOnClickListener(this);

Finally:

//Add this method to your Activity
@Override 
public void onClick(View v) {
    if(v==tglbuttn) {
        if(v.isChecked())
            play();
        else                
            stop();
        tglbuttn.setChecked(!tglbuttn.isChecked()); //It will be toggled
     }
}
iTurki
  • 16,292
  • 20
  • 87
  • 132