pingboo23
pingboo23

Reputation: 137

Stop and Play MediaPlayer between activities

I have these 3 classes namely MainActivity, Options and the Play. When the user clicks the Options button, the user can decide if the he/she wants to play with audio or not. But the default is, the audio will play although the user will not click button on the Options.

I found some codes here in StackOverflow, but I don't know how to stop the audio if the user decides to stop the audio after deciding to play it. (Going to the options again)

Here's the code so far:

MainActivity.class (the Options)

public class MainActivity extends Activity {

    public MediaPlayer mp = null;

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

        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

                @Override
            public void onClick(View v) {
                Intent a = new Intent(MainActivity.this, Play.class);
                startActivity(a);



            }

        });
    }
}

Play.java

public class Play extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play);

        Button back = (Button) findViewById (R.id.button1);

        back.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent menu = new Intent (getApplicationContext(), Menu.class);
                startActivity(menu);
            }

        });


    MainActivity z = new MainActivity();
    MediaPlayer mp  = z.mp;

    if (mp == null) {
        mp = MediaPlayer.create(Play.this, R.raw.math_game);
        mp.start();
    }

    else if (mp.isPlaying()) {
        mp.stop();
        mp = null;
    }
    }

}

and the Main Menu

public class Menu extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        Button play, opt;
        play = (Button) findViewById (R.id.play);
        opt = (Button) findViewById (R.id.opt);

        play.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent play = new Intent (Menu.this, Play.class);
                startActivity(play);
            }


        });

        opt.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent play = new Intent (Menu.this, MainActivity.class);
                startActivity(play);
            }


        });     
    }

}

Thankyou :))

Upvotes: 0

Views: 560

Answers (1)

josh527
josh527

Reputation: 6999

Look into the Android Activity Lifecycle (http://developer.android.com/training/basics/activity-lifecycle/index.html) and you will find there are more activity overrides.. i.e. onPause, onResume, onStop, and onDestroy. I imagine you will want to stop the music in onPause for the Play Activity.

Upvotes: 1

Related Questions