I am making an android game, but however, the game is functioning like it should, but i am experiencing some performance issues. I think it has something to do with the sound.
Cause each time i touch the screen, it makes a sound. I am using the standard MediaPlayer. The method is onTouchEvent() and onPlaySound1(). Could you please help me with an alternate solution for playing the sound? Thank you so much in advance! It would be nice if you also came up with some suggestions on how i can improve my code. Take a look at my code here:
package com.mycompany.mygame;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;
public class ExampleView extends SurfaceView implements SurfaceHolder.Callback
{
class ExampleThread extends Thread
{
private ArrayList<Parachuter> parachuters;
private Bitmap parachuter;
private Bitmap background;
private Paint black;
private boolean running;
private SurfaceHolder mSurfaceHolder;
private Context mContext;
private Context mContext1;
private Handler mHandler;
private Handler mHandler1;
private GameScreenActivity mActivity;
private long frameRate;
private boolean loading;
public float x;
public float y;
public float x1;
public float y1;
public MediaPlayer mp1;
public MediaPlayer mp2;
public int parachuterIndexToResetAndDelete;
public int canvasGetWidth;
public int canvasGetWidth1;
public int canvasGetHeight;
public int livesLeftValue;
public int levelValue = 1;
public int levelValue1;
public int parachutersDown;
public int difficultySet;
public boolean isSpecialAttackAvailible;
public ExampleThread(SurfaceHolder sHolder, Context context, Handler handler)
{
mSurfaceHolder = sHolder;
mHandler = handler;
mHandler1 = handler;
mContext = context;
mActivity = (GameScreenActivity) context;
parachuters = new ArrayList<Parachuter>();
parachuter = BitmapFactory.decodeResource(getResources(), R.drawable.parachuteman);
black = new Paint();
black.setStyle(Paint.Style.FILL);
black.setColor(Color.GRAY);
background = BitmapFactory.decodeResource(getResources(), R.drawable.gamescreenbackground);
running = true;
// This equates to 26 frames per second.
frameRate = (long) (1000 / 26);
loading = true;
mp1 = MediaPlayer.create(getContext(), R.raw.bombsound);
}
@Override
public void run()
{
while (running)
{
Canvas c = null;
try
{
c = mSurfaceHolder.lockCanvas();
synchronized (mSurfaceHolder)
{
long start = System.currentTimeMillis();
doDraw(c);
long diff = System.currentTimeMillis() - start;
if (diff < frameRate)
Thread.sleep(frameRate - diff);
}
} catch (InterruptedException e)
{
}
finally
{
if (c != null)
{
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
protected void doDraw(Canvas canvas)
{
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), black);
//Draw
for (int i = 0; i < parachuters.size(); i++)
{
canvas.drawBitmap(parachuter, parachuters.get(i).getX(), parachuters.get(i).getY(), null);
parachuters.get(i).tick();
}
//Remove
for (int i = 0; i < parachuters.size(); i++)
{
if (parachuters.get(i).getY() > canvas.getHeight()) {
parachuters.remove(i);
onPlaySound();
checkLivesLeftValue();
checkAmountOfParachuters();
} else if(parachuters.get(i).isTouched()) {
parachuters.remove(i);
} else{
//Do nothing
}
}
}
public void loadBackground(Canvas canvas) {
//Load background
canvas.drawBitmap(background, 0, 0, black);
}
public void checkAmountOfParachuters()
{
mHandler.post(new Runnable() {
@Override
public void run() {
if(parachuters.isEmpty()) {
levelValue = levelValue + 1;
Toast.makeText(getContext(), "New level! " + levelValue, 15).show();
if (levelValue == 3) {
drawParachutersGroup1();
drawParachutersGroup2();
drawParachutersGroup3();
drawParachutersGroup4();
} else if (levelValue == 5) {
drawParachutersGroup1();
drawParachutersGroup2();
drawParachutersGroup3();
drawParachutersGroup4();
drawParachutersGroup5();
} else if (levelValue == 7) {
drawParachutersGroup1();
drawParachutersGroup2();
drawParachutersGroup3();
drawParachutersGroup4();
drawParachutersGroup5();
drawParachutersGroup6();
} else if (levelValue == 9) {
//Draw 7 groups of parachuters
drawParachutersGroup1();
drawParachutersGroup2();
drawParachutersGroup3();
drawParachutersGroup4();
drawParachutersGroup5();
drawParachutersGroup6();
drawParachutersGroup1();
} else if (levelValue > 9) {
//Draw 7 groups of parachuters
drawParachutersGroup1();
drawParachutersGroup2();
drawParachutersGroup3();
drawParachutersGroup4();
drawParachutersGroup5();
drawParachutersGroup6();
drawParachutersGroup1();
} else {
//Draw normal 3 groups of parachuters
drawParachutersGroup1();
drawParachutersGroup2();
drawParachutersGroup3();
}
} else {
//Do nothing
}
}
});
}
private void checkLivesLeftValue() {
mHandler.post(new Runnable() {
@Override
public void run() {
Log.d("checkLivesLeftValue", "lives = " + livesLeftValue);
// TODO Auto-generated method stub
if (livesLeftValue == 3) {
//Message to display: "You lost!
Log.d("checkLivesLeftValue", "calling onMethod now");
parachuters.removeAll(parachuters);
onMethod();
}
else if (livesLeftValue == 2) {
Toast.makeText(getContext(), "Lives left=1", 15).show();
livesLeftValue = livesLeftValue + 1;
Log.d("checkLivesLeftValue", "increased lives to " + livesLeftValue);
}
else if (livesLeftValue == 1) {
Toast.makeText(getContext(), "Lives left=2", 15).show();
livesLeftValue = livesLeftValue + 1;
Log.d("checkLivesLeftValue", "increased lives to " + livesLeftValue);
}
else {
//Set livesLeftValueText 3
Toast.makeText(getContext(), "Lives left=3", 15).show();
livesLeftValue = livesLeftValue + 1;
Log.d("checkLivesLeftValue", "increased lives to " + livesLeftValue);
}
}
});
}
public void onMethod() {
mHandler.post(new Runnable() {
@Override
public void run() {
try {
Toast.makeText(getContext(), "You lost!", 15).show();
livesLeftValue = 0;
//Tell the user that he lost:
android.content.Context ctx = mContext;
Intent i = new Intent(ctx, playerLostMessageActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("KEY","You got to level " + levelValue + " And you shot down " + parachutersDown + " parachuters");
i.putExtra("levelValue", levelValue);
ctx.startActivity(i);
System.exit(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//Exit activity and start playerLostMessageActivity
Toast.makeText(getContext(), "You lost!", 15).show();
livesLeftValue = 0;
//Tell the user that he lost:
android.content.Context ctx = mContext;
Intent i = new Intent(ctx, playerLostMessageActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("KEY","You got to level " + levelValue + " And you shot down " + parachutersDown + " parachuters");
i.putExtra("levelValue", levelValue);
System.exit(0);
ctx.startActivity(i);
System.exit(0);
}
}
});
}
public void onPlaySound()
{
try {
mp1.start();
} catch (Exception e) {
e.printStackTrace();
mp1.release();
}
}
public void onDestroy()
{
try {
parachuters.removeAll(parachuters);
mp1.stop();
mp1.release();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onPlaySound1()
{
try {
mp2 = MediaPlayer.create(getContext(), R.raw.airriflesoundeffect);
mp2.start();
} catch (Exception e) {
e.printStackTrace();
mp2.release();
}
}
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() != MotionEvent.ACTION_DOWN)
releaseMediaPlayer();
x1 = event.getX();
y1 = event.getY();
checkAmountOfParachuters();
removeParachuter();
return false;
}
public void releaseMediaPlayer()
{
try {
mp1.release();
} catch (Exception e) {
e.printStackTrace();
}
}
public void removeParachuter()
{
try {
for (Parachuter p: parachuters) {
if (x1 > p.getX() && x1 < p.getX() + parachuter.getWidth() && y1 > p.getY() && y1 < p.getY() + parachuter.getHeight()) {
p.setTouched(true);
onPlaySound1();
parachutersDown = parachutersDown + 1;
p.setTouched(false);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void initiateDrawParachuters()
{
drawParachutersGroup1();
}
public void drawParachutersGroup1() {
// TODO Auto-generated method stub
//Parachuter group nr. 1
//Parachuter nr. 2
x = 75;
y = 77;
Parachuter p1 = new Parachuter(x, y);
parachuters.add(p1);
//Parachuter nr.1
x = 14;
y = 28;
Parachuter p = new Parachuter(x, y);
parachuters.add(p);
//Parachuter nr. 3
x = 250;
y = 94;
Parachuter p3 = new Parachuter(x, y);
parachuters.add(p3);
//Parachuter nr. 3
x = 275;
y = 80;
Parachuter p2 = new Parachuter(x, y);
parachuters.add(p2);
//Parachuter nr. 5
x = 280;
y = 163;
Parachuter p5 = new Parachuter(x, y);
parachuters.add(p5);
x = 125;
y = 118;
Parachuter p4 = new Parachuter(x, y);
parachuters.add(p4);
//Parachuter nr. 7
x = 126;
y = 247;
Parachuter p7 = new Parachuter(x, y);
parachuters.add(p7);
//Parachuter nr. 6
x = 123;
y = 77;
Parachuter p6 = new Parachuter(x, y);
parachuters.add(p6);
}
public void drawParachutersGroup2() {
// TODO Auto-generated method stub
//Parachuter group nr. 2
//Parachuter nr. 5
x = 153;
y = 166;
Parachuter p5 = new Parachuter(x, y);
parachuters.add(p5);
x = 133;
y = 123;
Parachuter p4 = new Parachuter(x, y);
parachuters.add(p4);
//Parachuter nr. 7
x = 170;
y = 213;
Parachuter p7 = new Parachuter(x, y);
parachuters.add(p7);
//Parachuter nr. 6
x = 190;
y = 121;
Parachuter p6 = new Parachuter(x, y);
parachuters.add(p6);
}
public void drawParachutersGroup3() {
// TODO Auto-generated method stub
//Parachuter group nr. 3
//Parachuter nr. 2
x = 267;
y = 115;
Parachuter p1 = new Parachuter(x, y);
parachuters.add(p1);
//Parachuter nr.1
x = 255;
y = 183;
Parachuter p = new Parachuter(x, y);
parachuters.add(p);
//Parachuter nr. 3
x = 170;
y = 280;
Parachuter p3 = new Parachuter(x, y);
parachuters.add(p3);
//Parachuter nr. 3
x = 116;
y = 80;
Parachuter p2 = new Parachuter(x, y);
parachuters.add(p2);
//Parachuter nr. 5
x = 67;
y = 112;
Parachuter p5 = new Parachuter(x, y);
parachuters.add(p5);
x = 260;
y = 89;
Parachuter p4 = new Parachuter(x, y);
parachuters.add(p4);
//Parachuter nr. 7
x = 260;
y = 113;
Parachuter p7 = new Parachuter(x, y);
parachuters.add(p7);
//Parachuter nr. 6
x = 178;
y = 25;
Parachuter p6 = new Parachuter(x, y);
parachuters.add(p6);
}
public void drawParachutersGroup4() {
// TODO Auto-generated method stub
//Parachuter group nr. 1
//Parachuter nr. 2
x = 75;
y = 166;
Parachuter p1 = new Parachuter(x, y);
parachuters.add(p1);
//Parachuter nr.1
x = 118;
y = 94;
Parachuter p = new Parachuter(x, y);
parachuters.add(p);
//Parachuter nr. 3
x = 38;
y = 55;
Parachuter p3 = new Parachuter(x, y);
parachuters.add(p3);
//Parachuter nr. 3
x = 57;
y = 18;
Parachuter p2 = new Parachuter(x, y);
parachuters.add(p2);
//Parachuter nr. 5
x = 67;
y = 119;
Parachuter p5 = new Parachuter(x, y);
parachuters.add(p5);
x = 217;
y = 113;
Parachuter p4 = new Parachuter(x, y);
parachuters.add(p4);
//Parachuter nr. 7
x = 245;
y = 234;
Parachuter p7 = new Parachuter(x, y);
parachuters.add(p7);
//Parachuter nr. 6
x = 239;
y = 44;
Parachuter p6 = new Parachuter(x, y);
parachuters.add(p6);
}
public void drawParachutersGroup5() {
// TODO Auto-generated method stub
//Parachuter group nr. 1
//Parachuter nr. 2
x = 59;
y = 120;
Parachuter p1 = new Parachuter(x, y);
parachuters.add(p1);
//Parachuter nr.1
x = 210;
y = 169;
Parachuter p = new Parachuter(x, y);
parachuters.add(p);
//Parachuter nr. 3
x = 199;
y = 138;
Parachuter p3 = new Parachuter(x, y);
parachuters.add(p3);
//Parachuter nr. 3
x = 22;
y = 307;
Parachuter p2 = new Parachuter(x, y);
parachuters.add(p2);
//Parachuter nr. 5
x = 195;
y = 22;
Parachuter p5 = new Parachuter(x, y);
parachuters.add(p5);
x = 157;
y = 132;
Parachuter p4 = new Parachuter(x, y);
parachuters.add(p4);
//Parachuter nr. 7
x = 150;
y = 183;
Parachuter p7 = new Parachuter(x, y);
parachuters.add(p7);
//Parachuter nr. 6
x = 130;
y = 20;
Parachuter p6 = new Parachuter(x, y);
parachuters.add(p6);
}
public void drawParachutersGroup6() {
// TODO Auto-generated method stub
//Parachuter group nr. 1
//Parachuter nr. 2
x = 10;
y = 10;
Parachuter p1 = new Parachuter(x, y);
parachuters.add(p1);
//Parachuter nr.1
x = 20;
y = 20;
Parachuter p = new Parachuter(x, y);
parachuters.add(p);
//Parachuter nr. 3
x = 30;
y = 30;
Parachuter p3 = new Parachuter(x, y);
parachuters.add(p3);
//Parachuter nr. 3
x = 60;
y = 60;
Parachuter p2 = new Parachuter(x, y);
parachuters.add(p2);
//Parachuter nr. 5
x = 90;
y = 90;
Parachuter p5 = new Parachuter(x, y);
parachuters.add(p5);
x = 120;
y = 120;
Parachuter p4 = new Parachuter(x, y);
parachuters.add(p4);
//Parachuter nr. 7
x = 150;
y = 150;
Parachuter p7 = new Parachuter(x, y);
parachuters.add(p7);
//Parachuter nr. 6
x = 180;
y = 180;
Parachuter p6 = new Parachuter(x, y);
parachuters.add(p6);
}
public void drawParachuters()
{
Parachuter p = new Parachuter(x, y);
parachuters.add(p);
Toast.makeText(getContext(), "x=" + x + " y=" + y, 15).show();
}
public void setRunning(boolean bRun)
{
running = bRun;
}
public boolean getRunning()
{
return running;
}
}
/** Handle to the application context, used to e.g. fetch Drawables. */
private Context mContext;
/** Pointer to the text view to display "Paused.." etc. */
private TextView mStatusText;
/** The thread that actually draws the animation */
private ExampleThread eThread;
public ExampleView(Context context)
{
super(context);
// register our interest in hearing about changes to our surface
SurfaceHolder holder = getHolder();
holder.addCallback(this);
// create thread only; it's started in surfaceCreated()
eThread = new ExampleThread(holder, context, new Handler()
{
@Override
public void handleMessage(Message m)
{
// mStatusText.setVisibility(m.getData().getInt("viz"));
// mStatusText.setText(m.getData().getString("text"));
}
});
setFocusable(true);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
return eThread.onTouchEvent(event);
}
public ExampleThread getThread()
{
return eThread;
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder)
{
if (eThread.getState() == Thread.State.TERMINATED)
{
eThread = new ExampleThread(getHolder(), getContext(), getHandler());
eThread.start();
}
else
{
eThread.start();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
boolean retry = true;
eThread.setRunning(false);
while (retry)
{
try
{
eThread.join();
retry = false;
}
catch (InterruptedException e)
{
}
}
}
}