Hey guys, I'm really new to programming and this is my first app attempt. I have a table layout with buttons and I want each button to play a sound (ie soundboard).
I can handle that fine, but I have 40 buttons now and each one I am calling a new instance of mediaplayer, so eventually I end up with too many and sound stops playing.
I have the following code for each button:
MediaPlayer MP1 = MediaPlayer.Create (this, Resource.Raw.sound01);
Button button1 =FindViewById<Button>(Resource.Id.button1);
button1.Click+=(object sender,EventArgs e)=>{
MP1.Start();
MP1.Release();};
MediaPlayer MP2 = MediaPlayer.Create (this, Resource.Raw.sound01);
Button button2 =FindViewById<Button>(Resource.Id.button2);
button2.Click+=(object sender,EventArgs e)=>{
MP2.Start();
MP2.Release();
};
etc.
I realize this is very repetitive and bad code. I have been told I need to use a global instance of mediaplayer and release it after the sound is played but I have no idea how to do either of those things.
I also found a suggestion to make something like this:
private int buttonIds = { R.id.button01, R.id.button02, R.id.button03,
R.id.button04, etc };
privateint soundIds ={ R.raw.sound01, R.raw.sound02, R.raw.sound03,
R.raw.sound04, etc };
Any help with direct code examples would really help me out a lot, thank you
I can handle that fine, but I have 40 buttons now and each one I am calling a new instance of mediaplayer, so eventually I end up with too many and sound stops playing.
I have the following code for each button:
MediaPlayer MP1 = MediaPlayer.Create (this, Resource.Raw.sound01);
Button button1 =FindViewById<Button>(Resource.Id.button1);
button1.Click+=(object sender,EventArgs e)=>{
MP1.Start();
MP1.Release();};
MediaPlayer MP2 = MediaPlayer.Create (this, Resource.Raw.sound01);
Button button2 =FindViewById<Button>(Resource.Id.button2);
button2.Click+=(object sender,EventArgs e)=>{
MP2.Start();
MP2.Release();
};
etc.
I realize this is very repetitive and bad code. I have been told I need to use a global instance of mediaplayer and release it after the sound is played but I have no idea how to do either of those things.
I also found a suggestion to make something like this:
private int buttonIds = { R.id.button01, R.id.button02, R.id.button03,
R.id.button04, etc };
privateint soundIds ={ R.raw.sound01, R.raw.sound02, R.raw.sound03,
R.raw.sound04, etc };
Any help with direct code examples would really help me out a lot, thank you