I am trying to make an app that plays specific sounds on button click , I got like 100 buttons created statically not in an array or anything , and I assigned the sounds to each button correctly the problem is , after playing a number of buttons it gives me that error my questions are
- Any way I could possibly switch to generic code from this mess
- Anyway I could stop getting this error and the sounds to continue working on every button whatever the amount of sounds played??
Below is my code :
package com.example.buttonsdemo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button messageButton_0 = (Button) findViewById(R.id.akali);
final MediaPlayer mpButtonClick_0= MediaPlayer.create(this,R.raw.akali) ;
messageButton_0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mpButtonClick_0.start();
if(!mpButtonClick_0.isPlaying()){
mpButtonClick_0.stop();
mpButtonClick_0.release();
}
}
});
And it goes on like this for 100 more buttons or so any help plz
Logcat:
03-04 16:21:21.925: E/MediaPlayer(5769): error (-19, 0)
03-04 16:21:21.925: E/MediaPlayer(5769): stop called in state 0
03-04 16:21:21.925: E/MediaPlayer(5769): error (-38, 0)
03-04 16:21:22.067: W/MediaPlayer(5769): mediaplayer went away with unhandled events
03-04 16:21:22.067: W/MediaPlayer(5769): mediaplayer went away with unhandled events
03-04 16:21:22.115: D/AndroidRuntime(5769): Shutting down VM
03-04 16:21:22.115: W/dalvikvm(5769): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-04 16:21:22.145: E/AndroidRuntime(5769): FATAL EXCEPTION: main
03-04 16:21:22.145: E/AndroidRuntime(5769): java.lang.IllegalStateException
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.media.MediaPlayer._start(Native Method)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.media.MediaPlayer.start(MediaPlayer.java:1025)
03-04 16:21:22.145: E/AndroidRuntime(5769): at com.example.buttonsdemo.MainActivity$39.onClick(MainActivity.java:766)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.view.View.performClick(View.java:4204)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.view.View$PerformClick.run(View.java:17355)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.os.Handler.handleCallback(Handler.java:725)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.os.Handler.dispatchMessage(Handler.java:92)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.os.Looper.loop(Looper.java:137)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-04 16:21:22.145: E/AndroidRuntime(5769): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 16:21:22.145: E/AndroidRuntime(5769): at java.lang.reflect.Method.invoke(Method.java:511)
03-04 16:21:22.145: E/AndroidRuntime(5769): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-04 16:21:22.145: E/AndroidRuntime(5769): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-04 16:21:22.145: E/AndroidRuntime(5769): at dalvik.system.NativeStart.main(Native Method)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…