Reputation: 75
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
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)
Upvotes: 1
Views: 9148
Reputation: 5990
From the error messages, it is clear that a start
on MediaPlayer
is called when the same is in error state. From your messages,
03-04 16:21:21.925: E/MediaPlayer(5769): stop called in state 0
State 0 corresponds to MEDIA_PLAYER_STATE_ERROR
03-04 16:21:21.925: E/MediaPlayer(5769): error (-38, 0)
The error no. -38 corresponds to INVALID_OPERATION
from MediaPlayer::start
. Please note that INVALID_OPERATION
is set to -ENOSYS
which is -38 from errno.h
.
You may have to investigate why your MediaPlayer
is in an erroneous state.
Upvotes: 6
Reputation: 5578
You need to look at the documentation of MediaPlayer.start and the State Diagram.
You probably are calling the MediPlayer while it's in one of the invalid states ({Idle, Initialized, Stopped, Error}), which would cause the exception.
Upvotes: 2