Question

i have a simple app, but it seems it's not very simple. What i want to do is: when i press on Button 1 plays Song1, if i press on Button 2 -- then Song1 stops and Song 2 plays i can make this if i have only 2 buttons and 2 songs, but how can i make it when i have more? here is my code

    private MediaPlayer mp1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button Bot1 = (Button) findViewById(R.id.button1);
    Button Bot2 = (Button) findViewById(R.id.button2);
    Button Bot3 = (Button) findViewById(R.id.button3);
    Button Bot4 = (Button) findViewById(R.id.button4);
    Bot1.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            if(mp1.isPlaying()) {
                mp1.stop();}
            else
            {   if (mp1.isPlaying()){
                mp1.stop();
                mp1.release(); }

                mp1 = MediaPlayer.create(getApplicationContext(), R.raw.bath);
                mp1.start();
                }
        }
    });
    Bot2.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            if(mp1.isPlaying()) {
                mp1.stop();}
            else
            {   if (mp1.isPlaying())
                mp1.stop();
            mp1.release();
                mp1 = MediaPlayer.create(getApplicationContext(), R.raw.flush);
                mp1.start();}
        }
    });
    Bot3.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            if(mp1.isPlaying()) {
                mp1.stop();}
            else
            {if (mp1.isPlaying())
                mp1.stop();
            mp1.release();
                mp1 = MediaPlayer.create(getApplicationContext(), R.raw.tolflush);
                mp1.start();}
        }
    });
    Bot4.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            if(mp1.isPlaying()) {
                mp1.stop();}
            else
            {     if (mp1.isPlaying())
                mp1.stop();
            mp1.release();
                  mp1 = MediaPlayer.create(getApplicationContext(), R.raw.tolflushs);
                  mp1.start();
             } }
            });
    mp1 = MediaPlayer.create(this, R.raw.tolflushs);
    }
Was it helpful?

Solution 3

ok, i solved the nullpointexception and after this i got a illegalstateexception, now i made player to work. i knew which is the problem, and i know that the code is based on logic, but at that time my logic wasn't efficient, i got to sleep, and in the morning i allready had the answer in my head :)

  1. created private void stopPlayers() // (coders will say i'm crazy:)

        if (mp1.isPlaying()) { mp1.stop(); mp1.reset();}else
    if (mp2.isPlaying()){ mp2.stop(); mp2.reset();}else
    if (mp3.isPlaying()){ mp3.stop(); mp3.reset();}else
    if (mp4.isPlaying()){ mp4.stop(); mp4.reset();}else{ mp1.reset();
    
  2. in each OnClickListener add stopPlayers()

Bot4.setOnClickListener(new OnClickListener(){

    public void onClick(View v) {
        if(mp4.isPlaying()) {
            mp4.stop();}
        else
        {   
          stopPlayers();
            mp4 = MediaPlayer.create(getApplicationContext(), R.raw.tolflushs);
              mp4.start();
       } }
        });
   }

That's all, now if i have 10 buttons with 10 different songs i can play which song i want and the previous song will stop playing. and even if i'll hit the same button it will stop playing.

OTHER TIPS

When you press Button1

mp2.stop();mp3.stop();mp4.stop();

is called but these MediaPlayers have not been created yet. That's why you net a null pointer exception.

Instantiate all MediaPlayers outside of the OnClickListeners.

Also I would suggest not to use 4 MediaPlayers, cause you waste resources. Create only one and every time a button is clicked, check if it is playing, stop it, release it and recreate it:

if (mp1.isPlaying())
    mp1.stop();
mp1.release();
mp1 = MediaPlayer.create(getApplicationContext(), R.raw.flush);

To avoid the NullPointerException you could test in your conditions if the mp instance is not null like this if ((mp1 != null) && mp1.isPlaying()), and if you want to manage several buttons with several media players, you could use a Map.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top