0

I am trying to create a method of an image button. When it's clicked, the background music stops and the image button change to another image. When pressed again, it will return as if it was at the first time and replay the music.

I am trying a Boolean. When it's true, the music start and when it's false the music, but it doesn't work!

In addition, how I can make another activity play or stop the music depending on the main activity?

public class MainActivity extends AppCompatActivity {

    MediaPlayer mp;
    ImageButton SoundButton;
    ImageButton NoSoundButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SoundButton = new ImageButton(this);
        NoSoundButton = new ImageButton(this);
        /*---------Image Buttons--------*/

        SoundButton=(ImageButton) findViewById(R.id.sound);
        SoundButton.setVisibility(View.GONE);
        NoSoundButton=(ImageButton) findViewById(R.id.nosound);
        NoSoundButton.setVisibility(View.VISIBLE);

        /*---------Media Player--------*/

        mp = new MediaPlayer();
        mp = MediaPlayer.create(this, R.raw.aud);
        mp.setLooping(true);
        mp.start();
    }

    public void nosound(View view) {
        SoundButton.setVisibility(View.VISIBLE);
        NoSoundButton.setVisibility(View.INVISIBLE);
        mp.stop();
        mp.prepareAsync();
    }

    public void sound(View view) {
        SoundButton.setVisibility(View.INVISIBLE);
        NoSoundButton.setVisibility(View.VISIBLE);
        mp.start();
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahmad
  • 1,618
  • 5
  • 24
  • 46

1 Answers1

5

1) You have to change the location of the initialization of your MediaPlayer.

 MediaPlayer mp = new MediaPlayer();

    public class MainActivity extends AppCompatActivity {

        boolean SoundStatus;
        MediaPlayer mp;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mp = new MediaPlayer();
            mp = MediaPlayer.create(this, R.raw.aud);
            mp.setLooping(true);
            mp.start();
        }
            public void sound(View view) {
            SoundStatus = true;
            if (SoundStatus) {
                mp.stop();
                SoundStatus = false;
            }
            else {
                mp.start();
                SoundStatus = true;
            }
        }}

And of course make sure that the audio file exists.


2) How to pass a button state

You can pass a button state using Bundle between activities like following

Start activity 2

Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_NAME, VALUE);
startActivity(intent);

Get that value in activity 2 like

@Override
protected void onCreate(Bundle savedInstanceState) {
....
boolean value = getIntent().getExtras().getBoolean(EXTRA_VALUE);
}

and then do the same like above, to pass it to Activity 3.

Or

You can make a static variable in you Activity 1 and then access that from Activity 3.

Cédric Portmann
  • 1,014
  • 1
  • 9
  • 22
  • mp is just known inside 'onCreate' ! in the method 'sound' it's unknown – Ahmad Nov 20 '16 at 06:02
  • Well then put mediaplayer mp globally or pass the mediaplayer as parameter – Cédric Portmann Nov 20 '16 at 08:42
  • updated my code.. try it now, btw did you notice that you are never calling the method 'sound'? – Cédric Portmann Nov 20 '16 at 12:01
  • it's called through 'onClick', I tried your code and stopped when I pressed the image button but when I pressed it again the music never played again! @Cédric Portmann – Ahmad Nov 20 '16 at 13:21
  • Consider to put mp.reset(); after the mp.stop() method. – Cédric Portmann Nov 20 '16 at 13:32
  • ImageButton SoundButton = new ImageButton(this); ImageButton NoSoundButton = new ImageButton(this); are at the wrong postion. they have to be inside the oncreate method – Cédric Portmann Nov 20 '16 at 13:54
  • Alright. Put only: ImageButton SoundButton; and ImageButton NoSoundButton; Above the onCreate method. Leave the rest and it should work – Cédric Portmann Nov 20 '16 at 14:00
  • It worked well, Thanks! but how I can make the music play in another activity or stop it if the button is pressed? @Cédric Portmann – Ahmad Nov 20 '16 at 14:59
  • For more information check out: http://stackoverflow.com/questions/34161483/how-to-change-switch-toggle-button-state-from-another-activity – Cédric Portmann Nov 20 '16 at 16:32
  • or: http://stackoverflow.com/questions/32110178/how-to-get-switch-button-status-and-pass-to-another-activity-using-shared-prefer but I wouldnt do it this way.. or: http://stackoverflow.com/questions/21824343/is-it-possible-to-save-the-state-of-a-button-in-one-activity-and-then-pass-it-on – Cédric Portmann Nov 20 '16 at 16:33
  • Glad I could help. Btw I propose you to have a closer look on Java programming in general, because I noticed that your problem was more of a basic java problem rather than an android problem. Have a good day – Cédric Portmann Nov 21 '16 at 12:18