mert alex mert alex - 5 months ago 17
Android Question

Music button problems

I have trouble with buttons. I have 31 different mp3 file. I add 31 button. When I pressed 29th button 30 and 31 isn't working in my phone. Emulator working is fine. I have one more problem, when I came mainactivity and then again join my mp3 page sometimes aren't work all buttons.

public class voice extends AppCompatActivity {

Button bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice);

bt = (Button) findViewById(R.id.button);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.voice1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
}
});


bt = (Button) findViewById(R.id.button2);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.voice2);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp2.start();
}
});

bt = (Button) findViewById(R.id.button3);
final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.voice3);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp3.start();
}
});

bt = (Button) findViewById(R.id.button4);
final MediaPlayer mp4 = MediaPlayer.create(this, R.raw.voice4);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp4.start();
}
});

bt = (Button) findViewById(R.id.button5);
final MediaPlayer mp5 = MediaPlayer.create(this, R.raw.voice5);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp5.start();
}
});

bt = (Button) findViewById(R.id.button6);
final MediaPlayer mp6 = MediaPlayer.create(this, R.raw.voice6);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp6.start();
}
});

bt = (Button) findViewById(R.id.button7);
final MediaPlayer mp7 = MediaPlayer.create(this, R.raw.voice7);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp7.start();
}
});

bt = (Button) findViewById(R.id.button8);
final MediaPlayer mp8 = MediaPlayer.create(this, R.raw.voice8);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp8.start();
}
});

bt = (Button) findViewById(R.id.button9);
final MediaPlayer mp9 = MediaPlayer.create(this, R.raw.voice9);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp9.start();
}
});

bt = (Button) findViewById(R.id.button10);
final MediaPlayer mp10 = MediaPlayer.create(this, R.raw.voice10);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp10.start();
}
});

bt = (Button) findViewById(R.id.button11);
final MediaPlayer mp11 = MediaPlayer.create(this, R.raw.voice11);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp11.start();
}
});

bt = (Button) findViewById(R.id.button12);
final MediaPlayer mp12 = MediaPlayer.create(this, R.raw.voice12);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp12.start();
}
});

bt = (Button) findViewById(R.id.button13);
final MediaPlayer mp13 = MediaPlayer.create(this, R.raw.voice13);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp13.start();
}
});

bt = (Button) findViewById(R.id.button14);
final MediaPlayer mp14 = MediaPlayer.create(this, R.raw.voice14);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp14.start();
}
});

bt = (Button) findViewById(R.id.button15);
final MediaPlayer mp15 = MediaPlayer.create(this, R.raw.voice15);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp15.start();
}
});

bt = (Button) findViewById(R.id.button16);
final MediaPlayer mp16 = MediaPlayer.create(this, R.raw.voice16);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp16.start();
}
});

bt = (Button) findViewById(R.id.button17);
final MediaPlayer mp17 = MediaPlayer.create(this, R.raw.voice17);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp17.start();
}
});

bt = (Button) findViewById(R.id.button18);
final MediaPlayer mp18 = MediaPlayer.create(this, R.raw.voice18);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp18.start();
}
});

bt = (Button) findViewById(R.id.button19);
final MediaPlayer mp19 = MediaPlayer.create(this, R.raw.voice19);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp19.start();
}
});
bt = (Button) findViewById(R.id.button20);
final MediaPlayer mp20 = MediaPlayer.create(this, R.raw.voice20);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp20.start();
}
});

bt = (Button) findViewById(R.id.button21);
final MediaPlayer mp21 = MediaPlayer.create(this, R.raw.voice21);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp21.start();
}
});

bt = (Button) findViewById(R.id.button22);
final MediaPlayer mp22 = MediaPlayer.create(this, R.raw.voice22);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp22.start();
}
});

bt = (Button) findViewById(R.id.button23);
final MediaPlayer mp23 = MediaPlayer.create(this, R.raw.voice23);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp23.start();
}
});

bt = (Button) findViewById(R.id.button24);
final MediaPlayer mp24 = MediaPlayer.create(this, R.raw.voice24);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp24.start();
}
});

bt = (Button) findViewById(R.id.button25);
final MediaPlayer mp25 = MediaPlayer.create(this, R.raw.voice25);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp25.start();
}
});

bt = (Button) findViewById(R.id.button26);
final MediaPlayer mp26 = MediaPlayer.create(this, R.raw.voice26);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp26.start();
}
});

bt = (Button) findViewById(R.id.button27);
final MediaPlayer mp27 = MediaPlayer.create(this, R.raw.voice27);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp27.start();
}
});

bt = (Button) findViewById(R.id.button28);
final MediaPlayer mp28 = MediaPlayer.create(this, R.raw.voice28);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp28.start();
}
});

bt = (Button) findViewById(R.id.button29);
final MediaPlayer mp29 = MediaPlayer.create(this, R.raw.voice29);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp29.start();
}
});

bt = (Button) findViewById(R.id.button30);
final MediaPlayer mp30 = MediaPlayer.create(this, R.raw.voice30);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp30.start();
}
});

bt = (Button) findViewById(R.id.button31);
final MediaPlayer mp31 = MediaPlayer.create(this, R.raw.voice31);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp31.start();
}
});


}



}
}

Answer Source

You can check Media Player docs

The idea is, you don't need to create your players. Use just one media player but, release previous data before using next one:

 if (mplayer !=null) {
     mplayer.stop();
     mplayer.release();
     mplayer = null;
 }
 mplayer = new  MediaPlayer();
 mplayer.setDataSource(getApplicationContext(),
                  Uri.parse(CONSTANTS.RES_PREFIX + R.raw.id));
 mplayer.prepare();
 mplayer.start();

Your code:

public class voice extends AppCompatActivity {

Button bt;
MediaPlayer mplayer;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_voice);

        bt = (Button) findViewById(R.id.button);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             String path = "android.resource://" + getPackageName() + "/" + R.raw.voice1;
             startNewVoice(path);
            }
        });
   }
void startNewVoice(String pathToFile) {
   if (mplayer !=null) {
       mplayer.stop();
       mplayer.release();
       mplayer = null;
   }
   mplayer = new  MediaPlayer();
   try{
       mplayer.setDataSource(this, Uri.parse(pathToFile));
    } catch (IOException e) {
        e.printStackTrace();
    }
   mplayer.prepare();
   mplayer.start();
}

}

Look at onClick. You need create path for each buttons and send it to startNewVoice method. Don't forget approve if its work for you