How to get a preview of camera - java

I used that example to get preview from camera:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.TextureView;
import android.widget.FrameLayout;
import android.view.TextureView;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.ImageFormat;
import android.graphics.PixelFormat;
import android.graphics.SurfaceTexture;
public class MainActivity extends Activity implements
TextureView.SurfaceTextureListener, Camera.PreviewCallback {
static {
System.loadLibrary("JNIProcessor");
}
private final String TAG="LiveFeature";
private Camera mCamera;
private byte[] mVideoSource;
private TextureView mTextureView;
private String[] ResolutionList;
private Menu AppMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_feature);
mTextureView.setSurfaceTextureListener(this);
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture pSurface,
int pWidth, int pHeight) {
// Ignored
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture pSurface) {
// Ignored
}
#Override
public void onPreviewFrame(byte[] pData, Camera pCamera) {
}
}
It seems the Camera.PreviewCallback is deprecated and I should use android.hardware.camera2. The problem is I don't find preview callback function to get the raw data bytes.
I just want to grabe frame from camera without render it into a surface and to put it into a JNI function.

These steps to get a preview:
Add camera permission in manifest file (Manifest.permission.CAMERA)
Get camera instance with this method:
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
Log.e(TAG, e.getMessage());
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
Create a class that extends SurfaceView and let the class implement SurfaceHolder.Callback and pass your Camera instance in its constructor. Get the holder in the constructor with getHolder()
In method "surfaceCreated", set preview display and start preview
public void surfaceCreated(SurfaceHolder holder) {
try {
mCameraSource.start(holder);
mCamera.setPreviewDisplay(mHolder);
setWillNotDraw(false);
} catch (Exception e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
Stop preview and release camera when you don't need it anymore
mCamera.stopPreview();
mCamera.release();

Related

Parcelable encountered IOException when sending instance of an object to other activity

in the onCreate of activit-main i make an inistance of a class named XMPPConectio
at the constructor of the XMMPPConnection i take inistance of the activity-main and therefor i execute an Asychtask. in the progressUpdate i call triger function of activity-main. i the triger function i want to send inistance of XMPPconnectio to other activity by intent.putExtra but i get the error
Parcelable encountered IOException writing serializable object
the aim of doing all these is to have connection (when it is connected) to other activity.
please give me some sample code thank you
XMPPConectio class which implements Serializable:
public class XMPPConnectio implements Serializable {
XMPPTCPConnection connection;
String connectionMessages="";
connectionXMPP connectionXMPPAsynch;
MainActivity mainActivity;
public XMPPTCPConnection getXMPPConnectio ()
{
return connection;
}
public XMPPConnectio(MainActivity mainActivity)
{
this.mainActivity=mainActivity;
try
{
connectionXMPPAsynch =new connectionXMPP();
connectionXMPPAsynch.execute();
}
catch (Exception e)
{
}
}
class connectionXMPP extends AsyncTask<String,Void,String>
{
#Override
protected String doInBackground(String... params) {
connection = new XMPPTCPConnection(XMPPTCPConnectionConfiguration.builder()
.setServiceName("192.168.1.6")
.setUsernameAndPassword("ehsan", "123")
.setPort(9090)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setDebuggerEnabled(true)
.setCompressionEnabled(false).build());
connection.setUseStreamManagement(true);
connection.addConnectionListener(new ConnectionListener()
{
#Override
public void connected(XMPPConnection connection) {
Log.d("connected", "yes connected successfully : ");
publishProgress();
}
#Override
public void authenticated(XMPPConnection connection, boolean resumed) {
Log.d("connected","yes authenticated successfully : ");
}
#Override
public void connectionClosed() {
Log.d("connected","yes connectionClosed successfully : ");
}
#Override
public void connectionClosedOnError(Exception e) {
Log.d("connected","yes connectionClosedOnError : ");
}
#Override
public void reconnectionSuccessful() {
Log.d("connected","yes reconnection successfully : ");
}
#Override
public void reconnectingIn(int seconds) {
Log.d("connected","yes reconnectingIn : ");
}
#Override
public void reconnectionFailed(Exception e) {
Log.d("connected","yes reconnectionFailed : ");
}
});
connect();
return null;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
mainActivity.triger();
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("","onPostExecute");
}
private void connect()
{
try {
connection.connect();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
}
}
}
the Activity which make instance of XMPPConectio and cause Asychtask execution
package passargad.ehsan;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.Socket;
public class MainActivity extends ActionBarActivity implements Serializable {
private Socket socket;
private String serverIpAddress = "192.168.1.6";
XMPPConnectio xmppConnectio;
XMPPTCPConnection connection;
private static final int REDIRECTED_SERVERPORT = 6789;
FastFood fastFood;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=new Intent(this,SocketService.class);
startService(intent);
fastFood =new FastFood(this);
xmppConnectio=new XMPPConnectio(this);
}
// this is the function which is called (when connection is done ) by //onProgressUpdate of Asychtask
public void triger()
{
try {
Intent intent= new Intent(this,chat.class);
intent.putExtra("XMPP",xmppConnectio);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
this is the activity which i want to have XMPPConnectio in there but the execution never reach to this
package passargad.ehsan;
import android.app.Application;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import java.io.IOException;
public class chat extends ActionBarActivity {
XMPPConnectio xmppConnectio;
XMPPTCPConnection connection;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
xmppConnectio=(XMPPConnectio)getIntent().getSerializableExtra("XMPP");
Log.d("","done");
}
}
i tried to make the question crystal clear by giving all the code. as i said the goal is to have the connection in all activities when connection is connected.
thank you .
if i want to have connection object accessible in every activities one of the solutions is using bundle and send the bundle with intent. but the actual problem is not having bundle option in API level 10 (android 2.3). the other problem was ,not having the option to use serializing and putExtra for intent because XMPPTCPConnection does not implemet serializable. so i used the third option which was defining the object static.
so i changed the class XMPPConectio to this
import android.app.Application;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatManagerListener;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import java.io.IOException;
import java.io.Serializable;
public class XMPPConnectio {
XMPPTCPConnection connection;
String connectionMessages="";
connectionXMPP connectionXMPPAsynch;
MainActivity mainActivity;
public XMPPTCPConnection getXMPPConnectio ()
{
return connection;
}
public XMPPConnectio(MainActivity mainActivity)
{
this.mainActivity=mainActivity;
try
{
connectionXMPPAsynch =new connectionXMPP();
connectionXMPPAsynch.execute();
}
catch (Exception e)
{
}
}
class connectionXMPP extends AsyncTask<String,Void,String>
{
#Override
protected String doInBackground(String... params) {
connection = new XMPPTCPConnection(XMPPTCPConnectionConfiguration.builder()
.setServiceName("192.168.1.6")
.setUsernameAndPassword("ehsan", "123")
.setPort(9090)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setDebuggerEnabled(true)
.setCompressionEnabled(false).build());
connection.setUseStreamManagement(true);
connection.addConnectionListener(new ConnectionListener()
{
#Override
public void connected(XMPPConnection connection) {
Log.d("connected","yes connected successfully : ");
publishProgress();
}
#Override
public void authenticated(XMPPConnection connection, boolean resumed) {
Log.d("connected","yes authenticated successfully : ");
}
#Override
public void connectionClosed() {
Log.d("connected","yes connectionClosed successfully : ");
}
#Override
public void connectionClosedOnError(Exception e) {
Log.d("connected","yes connectionClosedOnError : ");
}
#Override
public void reconnectionSuccessful() {
Log.d("connected","yes reconnection successfully : ");
}
#Override
public void reconnectingIn(int seconds) {
Log.d("connected","yes reconnectingIn : ");
}
#Override
public void reconnectionFailed(Exception e) {
Log.d("connected","yes reconnectionFailed : ");
}
});
connect();
return null;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
mainActivity.triger();
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("","onPostExecute");
}
private void connect()
{
try {
connection.connect();
// connection.login();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
}
}
}
the Activity which make instance of XMPPConectio and cause Asychtask execution
package passargad.ehsan;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.Socket;
public class MainActivity extends ActionBarActivity {
private Socket socket;
private String serverIpAddress = "192.168.1.6";
XMPPConnectio xmppConnectio;
public static XMPPTCPConnection connection;
private static final int REDIRECTED_SERVERPORT = 6789;
FastFood fastFood;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=new Intent(this,SocketService.class);
startService(intent);
fastFood =new FastFood(this);
//this is
xmppConnectio=new XMPPConnectio(this);
}
public void triger()
{
connection=xmppConnectio.getXMPPConnectio();
Intent intent= new Intent(this,chat.class);
startActivity(intent);
}
}
this is the activity which we can access the connection there
package passargad.ehsan;
import android.app.Application;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import java.io.IOException;
public class chat extends ActionBarActivity {
XMPPTCPConnection connection;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
//this is
connection=MainActivity.connection;
connection.addConnectionListener(new ConnectionListener() {
#Override
public void connected(XMPPConnection connection) {
Log.d("","connected");
}
#Override
public void authenticated(XMPPConnection connection, boolean resumed) {
Log.d("","6");
}
#Override
public void connectionClosed() {
Log.d("","5");
}
#Override
public void connectionClosedOnError(Exception e) {
Log.d("","4");
}
#Override
public void reconnectionSuccessful() {
Log.d("","3");
}
#Override
public void reconnectingIn(int seconds) {
Log.d("","2");
}
#Override
public void reconnectionFailed(Exception e) {
Log.d("","1");
}
});
try {
connection.login();
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Android: Constantly updating TextView with a new value

I need some suggestions as to how I can update a Textview located in the XML with a value generated through my code.
The program draws a straight line through the canvas, I want the textView to reflect the line's tip x-value.
My code as follows:
package com.example.threadexperiment1;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
lineDrawing InfiniteLine;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InfiniteLine = new lineDrawing (this);
setContentView(R.layout.activity_main);
Thread thread = new Thread(InfiniteLine);
thread.start();
RelativeLayout RL1 = (RelativeLayout)findViewById(R.id.RelativeLayout1);
RL1.addView(InfiniteLine);
}
public class lineDrawing extends View implements Runnable {
float x=100, y=100;
Paint lineColour = new Paint ();
TextView TV1 = (TextView)findViewById(R.id.textView1);
//Constructor
public lineDrawing(Context context) {
super(context);
lineColour.setColor(Color.BLACK);
}
//ondraw codes
#Override
public void onDraw (Canvas canvas){
canvas.drawLine(0,0,x,y,lineColour);
if (x==500){
x=0;
y=0;
}
invalidate();
}
#Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(250);
}
catch (Exception e) {}
x+=10;
y+=10;
}
}
}
}
Try textView.setText(string) within the thread.

Issues With the OCR code and Java Code for ANDROID

here is my code to convert the picture taken from the camera into text using tesseract's java wrapper tess4j! i have included the jar files added them to my path! code does not show any syntax errors! i launch the default camera it starts i take picture and it is shown in the image view i declared however the editbox seems empty though it should show the result of OCR!
package your.apnakaam.namespace;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.graphics.Bitmap;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.Log;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.File;
import java.io.File;
import net.sourceforge.tess4j.*;
public class KaamsekhaActivity extends Activity
{
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
//**************************************************************************************************
private Intent data;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setContentView(R.layout.apna_layout);
Button capt_but = (Button)findViewById(R.id.capture_btn);
this.imageView = (ImageView)this.findViewById(R.id.picture);
capt_but.setOnClickListener(new View.OnClickListener()
{
//#Override
// TODO Auto-generated method stub
public void onClick(View v)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
void myfunction()
}
}
public void myfunction()
{
TextView disp = (TextView)findViewById(R.id.editText1);
File new_pic = (File) data.getExtras().get("data");
Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping
try
{
String result = instance.doOCR(new_pic);
disp.setText(result);
}
catch (TesseractException e)
{
System.err.println(e.getMessage());
}
}
}
I can't figure out whats wrong with the code!
The function doOCR takes a buffered image that is not possible while working in android! Bufferedimage uses IIOimage that is used in java but not in android!

Trying to get custom android camera working

I have developed a basic camera and am trying to get it working.
Here is the camera code
package com.example.camera;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Camera camera;
Object size;
public CameraView(Context context) {
super(context);
holder = this.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(this);
}
public Camera getCamera(){
return camera;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters params = camera.getParameters();//Getting paramaters.
if(size!=null){ //make sure we don't pull a NullException.
params.setPreviewSize(width, height);//gotta set the size, since we know it.
}
camera.setParameters(params);//gotta set the paramaters now.
camera.startPreview();//starting the preview.
}
public void surfaceCreated(SurfaceHolder arg0) {
try{
camera = Camera.open();//setting the camera up.
camera.setPreviewDisplay(holder);//making the display our current SurfaceHolder
} catch (IOException e){
e.printStackTrace();//printing out the error message if it happens.
}
}
public void surfaceDestroyed(SurfaceHolder arg0) {
camera.stopPreview();
camera.release();
camera = null;
}
}
Now Im trying to call the camera from my main method and I just cant seem to make it work
package com.example.camera;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class main {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
cam = cv.getCamera(); //notice how we used that method? ;)
cam.takePicture(null,null,PictureCallback_);
}
});
}
Camera.PictureCallback PictureCallback_ = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
InputStream is = new ByteArrayInputStream(imageData);
Bitmap bmp = BitmapFactory.decodeStream(is);
}
} ;
Any help is appreciated. Trying to call the original camera activity getCamera and then get the picture taken and put it into imageView1 any help is appreciated.
Try to do this in your onClick() method:
cam.autoFocus(new AutoFocusCallback(){
Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
public void onShutter() {
// If you want to play your own sound (otherwise, it plays the sound by default)
}
};
#Override
public void onAutoFocus(boolean arg0, Camera arg1) {
cam.takePicture(shutterCallback, null, PictureCallback_);
}
});
I have created a camera library project here you can use this library to call the camera and take pictures and save it or read code on how to use the camera. Sample is given you can check it out

MediaPlayer OnClicListener stops when starts playing a new one

I'm doing a soundboard app, this is the last thing I have to fix, thanks for the help!
I want that the sounds stops then i click on a new one.
package com.passaros;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.murdersquirrel.newboard.R;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;
public class newBoard extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Toast.makeText(this, "Long touch to save as Ringtones.", Toast.LENGTH_LONG).show();
AdView layout = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest();
adRequest.setTesting(true);
layout.loadAd(adRequest);
final MediaPlayer sound01 = MediaPlayer.create(this, R.raw.sound01);
final MediaPlayer sound02 = MediaPlayer.create(this, R.raw.sound02);
//more and more....
Button s01 = (Button) findViewById(R.id.button01);
s01.setText(this.getString(R.string.quote01));
s01.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
sound01.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
sound01.start();
}
});
registerForContextMenu(s01);
Thanks again!
Ignore this: I am just typing something because the website thinks that I did not use enough words to help with the code....
Try this .....
public void onClick(View v) {
try {
sound01.prepare();
sound01.start(); // Place your start() in try block.
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
How about trying to do a simple method that checks if the sound is playing, if true, to stop the sound and allow a new sound to play?
Also it could help that you use only one media player as you can just switch the sounds being played.

Categories