Here is a simple piano app and it works but there is a problem. After about 20 clicks (sometimes it is exactly 28 clicks) even I click the buttons it doesn't play any sound. The app doesnt crash or doesn't warn me about anything. It is just nothing . There is no sound. Do you have any idea?
package com.example.playaudio;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private MediaPlayer mp;
private MediaPlayer mp2;
private MediaPlayer mp3;
private MediaPlayer mp4;
private MediaPlayer mp5;
private MediaPlayer mp6;
private MediaPlayer mp7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
Button button1=(Button)findViewById(R.id.button_1);
Button button2=(Button)findViewById(R.id.button_2);
Button button3=(Button)findViewById(R.id.button_3);
Button button4=(Button)findViewById(R.id.button_4);
Button button5=(Button)findViewById(R.id.button_5);
Button button6=(Button)findViewById(R.id.button_6);
Button button7=(Button)findViewById(R.id.button_7);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
}
public void onClick(View v) {
int resId;
int resId2;
int resId3;
int resId4;
int resId5;
int resId6;
int resId7;
switch (v.getId()) {
case R.id.button_1:
resId = R.raw.a;
mp = MediaPlayer.create(this, resId);
mp.start();
break;
case R.id.button_2:
resId2 = R.raw.b;
mp2 = MediaPlayer.create(this, resId2);
mp2.start();
break;
case R.id.button_3:
resId3 = R.raw.c;
mp3 = MediaPlayer.create(this, resId3);
mp3.start();
break;
case R.id.button_4:
resId4 = R.raw.d;
mp4 = MediaPlayer.create(this, resId4);
mp4.start();
break;
case R.id.button_5:
resId5 = R.raw.e;
mp5 = MediaPlayer.create(this, resId5);
mp5.start();
break;
case R.id.button_6:
resId6 = R.raw.f;
mp6 = MediaPlayer.create(this, resId6);
mp6.start();
break;
case R.id.button_7:
resId7 = R.raw.p;
mp7 = MediaPlayer.create(this, resId7);
mp7.start();
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Looks like you're creating a new MediaPlayer instance to play each sound. You should either reuse them or clean them up.
From the documentation of the MediaPlayer.create() method:
Convenience method to create a MediaPlayer for a given resource id. On success, prepare() will already have been called and must not be called again.
When done with the MediaPlayer, you should call release(), to free the resources. If not released, too many MediaPlayer instances will result in an exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With