10

I have an activity with two play and pause buttons (currently invisible) and a seekbar. When I press the play button, the pause button should become visible, and when I press the pause button it should turn invisible.

How would I do that?

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;


    public class MainActivity extends Activity implements Runnable, OnClickListener, OnSeekBarChangeListener{
        private SeekBar seekBar;
        private Button startMedia;
        private Button pauseMedia;
        private MediaPlayer mp;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);             

            AudioControl();         

        }

        public void AudioControl(){
            seekBar = (SeekBar) findViewById(R.id.seekBar1);
            startMedia = (Button) findViewById(R.id.button1);
            pauseMedia = (Button) findViewById(R.id.button2);
            seekBar.setOnSeekBarChangeListener(this);
            startMedia.setOnClickListener(this);
            pauseMedia.setOnClickListener(this); 
        }



        public void run() {
            int currentPosition= 0;
            int total = mp.getDuration();
            while (mp!=null && currentPosition<total) {
                try {
                    Thread.sleep(1000);
                    currentPosition= mp.getCurrentPosition();
                } catch (InterruptedException e) {
                    return;
                } catch (Exception e) {
                    return;
                }            
                seekBar.setProgress(currentPosition);
            }
        }

        public void onClick(View v) {
            if (v.equals(startMedia)) {
                if (mp != null && mp.isPlaying()) return;
                if(seekBar.getProgress() > 0) {
                    mp.start();
                    return;
                }
                mp = MediaPlayer.create(MainActivity.this, R.raw.lone);
                mp.start();                     
                seekBar.setProgress(0);
                seekBar.setMax(mp.getDuration());
                new Thread(this).start();
            }

            if (v.equals(pauseMedia) && mp!=null) {
                mp.pause();
            }       

        }

        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            if(fromUser) mp.seekTo(progress);

        }
    }

I don't know where should I put blow code. please help me!

hossein
  • 123
  • 1
  • 1
  • 5

6 Answers6

24

I Use for any view:

 public void toggleView(View view){
 if(view.getVisibility()==View.GONE)
   view.setVisibility(View.VISIBLE);
 else if(view.getVisibility()==View.VISIBLE)
   view.setVisibility(View.GONE);
  }
Fabio Guerra
  • 722
  • 6
  • 13
6
startMedia = (Button) findViewById(R.id.button1);
pauseMedia = (Button) findViewById(R.id.button2);

//Onclick Event ...

//Logic
if (flag === 'start') {
startMedia.setVisibility(View.VISIBLE);
pauseMedia .setVisibility(View.INVISIBLE);
} else {
startMedia.setVisibility(View.INVISIBLE);
pauseMedia .setVisibility(View.VISIBLE);
}

GONE will make the layout manager ignore the dimensions of the widget. INVISIBLE will make it so the widget is not visible, but the layout manager will still treat it as if it is there.

If you are familiar with CSS,

  • setVisibility(View.INVISIBLE) is like CSS opacity: 0
  • setVisibility(View.GONE) is like CSS display: none
  • setVisibility(View.VISIBLE) is like CSS display: block
Dehodson
  • 449
  • 4
  • 14
d.danailov
  • 9,594
  • 4
  • 51
  • 36
2

Use this Simple Logic to Toggle Visibility...

 if (rvLocation.getVisibility()==View.VISIBLE)
        rvLocation.setVisibility(View.GONE);
 else
        rvLocation.setVisibility(View.VISIBLE);
Abhishek Sengupta
  • 2,938
  • 1
  • 28
  • 35
0

You can set your pauseMedia's visibility to invisible with the below code:

pauseMedia.setVisibility(View.INVISIBLE);

or to set it as visible:

pauseMedia.setVisiblity(View.VISIBLE);
KevinM
  • 1,799
  • 4
  • 28
  • 58
0

Maybe try something like this:

in the context of this function

controls

is the view that was inflated when the class was instantiated

private void toggleControls(){

    //private variable to store whether or not the view is showing
    if(controlsAreShowing){
       if(controls != null) {
           controls.setVisibility(View.INVISIBLE);
           controlsAreShowing = false;
       }
    }else{
        controls.setVisibility(View.VISIBLE);
        controlsAreShowing = true;
    }
}
Nlinscott
  • 786
  • 7
  • 11
0

I think following code may work,

play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Play", Toast.LENGTH_SHORT).show();
            mediaPlayer.start();
            pause.setEnabled(true);
            play.setEnabled(false);
        }
    });

For disabling pause button,

pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "pause", Toast.LENGTH_SHORT).show();
            mediaPlayer.pause();
            pause.setEnabled(false);
            play.setEnabled(true);
        }
    });

I think this code may work for your scenario.

Archana
  • 378
  • 3
  • 17