I am trying to make an app that plays sound when button clicks and I have like 100+ sounds and 100+ buttons. I have created the buttons and now am mapping it to the sounds to be played. Another thing, when clicking more than "x" number of times the mediaplayer
crashes. Any ideas on how to fix that??
Note: The code does not want to execute as it is right now, and I really don't understand why.
package com.example.buttonsdemo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener {
// Creating Sound arrays
int i = 0;
MediaPlayer[] mediaplayer = new MediaPlayer[120];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int z = 0; z < 121; z++) {
mediaplayer[z] = null;
}
// Creating Button Array
Button button[] = new Button[120];
for (int x = 0; x < 121; x++) {
button[x] = null;
button[x].setOnClickListener(this);
}
// Creating Media player array
mediaplayer[1] = MediaPlayer.create(this, R.raw.alistar);
mediaplayer[0] = MediaPlayer.create(this, R.raw.akali);
button[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.akali:
i = 0;
break;
case R.id.alistar:
i = 1;
break;
}
mediaplayer[i].start();
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Logcat:
03-05 01:10:34.674: E/AndroidRuntime(1541): FATAL EXCEPTION: main
03-05 01:10:34.674: E/AndroidRuntime(1541): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttonsdemo/com.example.buttonsdemo.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=120; index=120
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.os.Looper.loop(Looper.java:137)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-05 01:10:34.674: E/AndroidRuntime(1541): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 01:10:34.674: E/AndroidRuntime(1541): at java.lang.reflect.Method.invoke(Method.java:511)
03-05 01:10:34.674: E/AndroidRuntime(1541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-05 01:10:34.674: E/AndroidRuntime(1541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-05 01:10:34.674: E/AndroidRuntime(1541): at dalvik.system.NativeStart.main(Native Method)
03-05 01:10:34.674: E/AndroidRuntime(1541): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=120; index=120
03-05 01:10:34.674: E/AndroidRuntime(1541): at com.example.buttonsdemo.MainActivity.onCreate(MainActivity.java:28)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.Activity.performCreate(Activity.java:5104)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-05 01:10:34.674: E/AndroidRuntime(1541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-05 01:10:34.674: E/AndroidRuntime(1541): ... 11 more