• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Need help with making a soundboard and using mediaplayer

kabal3

Lurker
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
 
What is API you use? This is not a standard android java-code
You need to call setOnCompletionListener to instance of MediaPlayer, which created via MediaPlayer.create and in this listener you need to call MediaPlayer.release . This is in normal java-code
 
I don't use it. But in all cases you need to play sounds asynchronously and call release for MediaPlayer instance, when sound playing not needed anymore. In Java this can be done via playback listeners, you need to search something like this in xamarin
 
Back
Top Bottom