In my TitleScreen activity, I have...
@Override
public boolean onTouchEvent(MotionEvent event)
{
Log.d("MyActivity", "in onTouchEvent!");
MediaPlayer myplayer = MediaPlayer.create(TitleScreen.this, R.raw.mysound);
myplayer.start();
startActivity(new Intent("com.example.GAME"));
return super.onTouchEvent(event);
}
This was causing the sound to play 3 times in rapid succession upon tapping the screen, making the sound I wanted to play have a delayed echo. I checked the logs, and my "in onTouchEvent!" message was logged 3 times.
This activity is just a static image for a title screen, and the idea is you tap it to start the next activity, and when you do so, the specified sound should player.
For my particular issue, I was able to solve it by placing a global level int variable:
private static int playerInstances = 0;
and then wrapping my MediaPlayer lines with:
if (playerInstances == 0)
{
MediaPlayer myplayer = MediaPlayer.create(TitleScreen.this, R.raw.critical1);
myplayer.start();
playerInstances++;
}
This insured that that code only executed once. So my problem is solved. I'm just wondering why I was getting onTouchEvent three times in one tap.
What you can actually do on your onTouchEvent
(better than using the static variable) is:
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN) {
Log.d("MyActivity", "in onTouchEvent!");
MediaPlayer myPlayer = MediaPlayer.create(TitleScreen.this, R.raw.mysound);
myPlayer.start();
startActivity(new Intent("com.example.GAME"));
}
return super.onTouchEvent(event);
}