Abhishek
Abhishek

Reputation: 53

How to change Image of button when press and Back press?

I am working on live streaming. My problem is that when I press play button the image should change to stop button and when I press that stop button it should again change to Play button. I am very confused how to do that?? Here is my button click listener..

 btnPublish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            btnPublish.setImageResource(R.drawable.recordactive);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("rtmpUrl", rtmpUrl);
            editor.apply();
            mPublisher.startPublish(rtmpUrl);
            mPublisher.startCamera();
        }
    });

When I am pressing play button it is changing to stop image but when I am pressing that stop button my app crashes.. Because I have not written any code for that.. Please guide me how to do it. Basically After pressing that stop button I want to call this function..

   mPublisher.stopPublish();
                mPublisher.stopRecord();

Upvotes: 0

Views: 331

Answers (5)

Surendra Shrestha
Surendra Shrestha

Reputation: 1080

You have to set a variable to store the current state of button and then work accordingly. An example is given below

boolean isPlaying = false;

btnPublish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isPlaying){
              isPlaying = false;
              //what to do when pressing stop button
              //change your button image
              //stop camera and other things...

            }else{
              isPlaying = true;
              //what to do when pressing the play button
              btnPublish.setImageResource(R.drawable.recordactive);
              SharedPreferences.Editor editor = sp.edit();
              editor.putString("rtmpUrl", rtmpUrl);
              editor.apply();
              mPublisher.startPublish(rtmpUrl);
              mPublisher.startCamera();
            }

        }
    });

Upvotes: 0

priya raj
priya raj

Reputation: 362

    btnPublish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            btnPublish.setImageResource(R.drawable.recordactive);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("rtmpUrl", rtmpUrl);
            editor.apply();
            //Check what is the image embedded on the button. Assuming button is //ImageButton
            Integer integer = (Integer) btnPublish.getTag();
            integer = integer == null ? 0 : integer;
   switch(integer) {
    case R.drawable.recordactive:
     btnPublish.setDrawableResource(R.drawable.recordstop);  //On click of active, //it should change to recordstop first
     btnPublish.setTag(R.drawable.recordstop);
            mPublisher.startPublish(rtmpUrl);
            mPublisher.startCamera();
     break;
    case R.drawable.recordstop:
btnPublish.setDrawableResource(R.drawable.recordactive);  //On click of stop, 
//it should change to recordactive  first
     btnPublish.setTag(R.drawable.recordactive);
mPublisher.stopPublish();
                mPublisher.stopRecord();
    default:
btnPublish.setDrawableResource(R.drawable.recordactive);  //default is record //active
     btnPublish.setTag(R.drawable.recordactive);
     break;
  }

        }
    });

Code is a mock up code. Hope that helps!!!

Upvotes: 0

Abhishek
Abhishek

Reputation: 53

I did something like this....

        btnPublish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //rtmpUrl = efu.getText().toString();
            btnPublish1.setVisibility(View.VISIBLE);
            btnPublish.setVisibility(View.GONE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("rtmpUrl", rtmpUrl);
            editor.apply();
            mPublisher.startPublish(rtmpUrl);
            mPublisher.startCamera();
        }
    });



    btnPublish1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //rtmpUrl = efu.getText().toString();
            btnPublish.setVisibility(View.VISIBLE);
            btnPublish1.setVisibility(View.GONE);
            mPublisher.stopPublish();
            mPublisher.stopRecord();
        }
    });

Upvotes: 0

frmi
frmi

Reputation: 517

As Ronak said you would want to keep track of the current state. You can do that using a boolean variable. Something like this

boolean isRecordActive = false;
btnPublish.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (isRecordActive) {
            btnPublish.setImageResource(R.drawable.recordinactive);
            // stop streaming
        } else {
            btnPublish.setImageResource(R.drawable.recordactive);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("rtmpUrl", rtmpUrl);
            editor.apply();
            mPublisher.startPublish(rtmpUrl);
            mPublisher.startCamera();
        }

        isRecordActive != isRecordActive;
    }
});

Upvotes: 0

Dishonered
Dishonered

Reputation: 8841

boolean showing = false;

 btnPublish.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(showing){
            mPublisher.stopPublish();
            mPublisher.stopRecord();
            showing = false;
        }
        else
         {
            btnPublish.setImageResource(R.drawable.recordactive);
          SharedPreferences.Editor editor = sp.edit();
           editor.putString("rtmpUrl", rtmpUrl);
        editor.apply();
        mPublisher.startPublish(rtmpUrl);
        mPublisher.startCamera();
        showing = true;
         }
    }
});

Upvotes: 1

Related Questions