Android soundboard error
My tasks run through and it runs fine up to the last few sounds. for some odd reason it keeps force closing, I have looked and looked and don't get why. I took sounds awa开发者_开发百科y and did several other steps and I don't understand is there a size limit or something?
It is the second program it has done it on, any ideas?
MediaPlayer mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8, mp9, mp10, mp11, mp12,
mp13, mp14, mp15, mp16, mp17, mp18, mp19, mp20, mp21, mp22, mp23, mp24, mp25,
mp26, mp27, mp28;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//set up the button sound
mp1 = MediaPlayer.create(this, R.raw.backtoyou);
//button 1 coding
ImageButton Button1 = (ImageButton) findViewById (R.id.button01);
Button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mp1.start();
}
});
//longclick creates ringtone notification
//Button1.setLongClickable(true);
//set up the button sound
mp2 = MediaPlayer.create(this, R.raw.blow);
//button 1 coding
ImageButton Button2 = (ImageButton) findViewById (R.id.button02);
Button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mp2.start();
}
});
//set up the button sound
mp3 = MediaPlayer.create(this, R.raw.boomstick);
//button 1 coding
ImageButton Button3 = (ImageButton) findViewById (R.id.button03);
Button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mp3.start();
}
});
//set up the button sound
mp4 = MediaPlayer.create(this, R.raw.byebye);
//button 1 coding
ImageButton Button4 = (ImageButton) findViewById (R.id.button04);
Button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mp4.start();
}
});
.... And so on and so forth. So where is the error for the files and why does it keep force closing?
I suspect that having 28 MediaPlayer
objects is eating up a lot of memory. Consider using a MediaPlayer
objects that gets re-initialized with a new sound each time the button is pressed. Check out this article: http://www.stealthcopter.com/blog/2010/08/android-soundpool-vs-mediaplayer-focus-on-soundboards-and-memory-problems/
Yeah you definitely have FAR too many MediaPlayer objects and redundant code. The question I would ask is if you ever need sounds to overlap. For a soundboard, I would assume not (and really, if you did, I'd think that would be an annoying UI quirk -- one I've seen many times).
Try something to this effect:
MediaPlayer player = new MediaPlayer();
Resources res = getResources();
//just keep them in the same order, e.g. button01 is tied to backtoyou
int[] buttonIds = { R.id.button01, R.id.button02, R.id.button03 /*etc...*/ };
int[] soundIds = { R.raw.backtoyou, R.raw.blow, R.raw.boomstick /*etc...*/ };
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
//find the index that matches the button's ID, and then reset
//the MediaPlayer instance, set the data source to the corresponding
//sound effect, prepare it, and start it playing.
for(int i = 0; i < buttonIds.length; i++) {
if(v.getId() == buttonIds[i]) {
AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
player.reset();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
player.prepare();
player.start();
break;
}
}
}
};
//set the same listener for every button ID, no need
//to keep a reference to every button
for(int i = 0; i < buttonIds.length; i++) {
Button soundButton = (Button)findViewById(buttonIds[i]);
soundButton.setOnClickListener(listener);
}
精彩评论