Reputation: 1
ok i am developing an andoird app which is the game simon, i encountered a problem that when i move from the menu to the game. i want the game will play 3 sounds at the beggining of the game which is selected randomly and the app to play them 1 after another in 1 sec delay, but the problem is the app before the game backgound is loaded the sounds all play at the same time.. shortly everything doesn't work and i am desprate. in order to use the buttons, i created a class called column which contains the button from the view and the sound it makes and use the method play to activate the sound and change the image. the class Game for the logic of the game here is the code:
Column Class:
package com.example.thegreatsimon;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
public class Column {
ImageButton button;
SoundPool sound;
int soundId;
int srcColor,lightColor;
Context con;
public Column(ImageButton button,Context con)
{
sound=new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
this.button=button;
switch (button.getId())
{
case R.id.ibBlue:
{
soundId=sound.load(con,R.raw.a ,1);
srcColor=R.drawable.blue;
lightColor=R.drawable.lightblue;
} break;
case R.id.ibGreen:
{
soundId=sound.load(con,R.raw.b,1);
srcColor=R.drawable.green;
lightColor=R.drawable.lightgreen;
} break;
case R.id.ibRed:
{
soundId=sound.load(con,R.raw.c ,1);
srcColor=R.drawable.red;
lightColor=R.drawable.lightred;
} break;
case R.id.ibYellow:
{
soundId=sound.load(con,R.raw.d ,1);
srcColor=R.drawable.yellow;
lightColor=R.drawable.lightyellow;
} break;
}
button.setOnClickListener(new Listen());
}
class Listen implements OnClickListener
{
@Override
public void onClick(final View v)
{
play();
}
}
void play()
{
sound.play(soundId, 1, 1, 0, 0, 1) ;
button.setBackgroundResource(lightColor);
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
button.setBackgroundResource(srcColor);
}
}, 300);
}
}
The Game Class:
package com.example.thegreatsimon;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageButton;
public class Game extends Activity {
Column bYellow,bRed,bGreen,bBlue;
SoundPool a=new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
int diff;
int life=3;
Random rand= new Random();
Handler hand = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
diff = Settings.difficulty;
switch(diff)
{
case(0):
setContentView(R.layout.gameeasy);
break;
case(1):
setContentView(R.layout.game);
bYellow=new Column((ImageButton)findViewById(R.id.ibYellow),getApplicationContext());
break;
case(2):
setContentView(R.layout.game);
bYellow=new Column((ImageButton)findViewById(R.id.ibYellow),getApplicationContext());
break;
}
bRed=new Column((ImageButton)findViewById(R.id.ibRed),getApplicationContext());
bGreen=new Column((ImageButton)findViewById(R.id.ibGreen),getApplicationContext());
bBlue=new Column((ImageButton)findViewById(R.id.ibBlue),getApplicationContext());
EasyGame();
}
public boolean EasyGame()
{
int r;
ArrayList list = new ArrayList();
boolean proceed;
for(int i=0;i<3;i++)
{
r=rand.nextInt(3);
switch(r)
{
case 0:
{
list.add(bRed);
Sleep(bRed);
}break;
case 1:
{
list.add(bGreen);
Sleep(bGreen);
}break;
case 2:
{
list.add(bBlue);
Sleep(bBlue);
}break;
}
}
proceed=UserPlay();
/*for(int i=0;i<2;i++)
{
r=rand.nextInt(3);
switch(r)
{
case 0:
list.add(bRed);
break;
case 1:
list.add(bGreen);
break;
case 2:
list.add(bBlue);
break;
}
for(int j=0;j<list.size();j++)
{
((Column)list.get(j)).play();
Sleep();
}
proceed=UserPlay();
}*/
return true;
}
public void MediumGame()
{
}
public void HardGame()
{
}
public void Sleep(final Column c)
{
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
c.play();
}
}, 1000);
}
public boolean UserPlay()
{
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Upvotes: 0
Views: 8075
Reputation: 3033
Look this code, i would have done so:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TestActivity extends Activity {
TextView timerTextView;
long startTime = 0;
//runs without a timer by reposting this handler at the end of the runnable
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
@Override
public void run() {
long millis = System.currentTimeMillis() - startTime;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
timerTextView.setText(String.format("%d:%02d", minutes, seconds));
timerHandler.postDelayed(this, 1000);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
timerTextView = (TextView) findViewById(R.id.timerTextView);
Button b = (Button) findViewById(R.id.button);
b.setText("start");
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button) v;
if (b.getText().equals("stop")) {
timerHandler.removeCallbacks(timerRunnable);
b.setText("start");
} else {
startTime = System.currentTimeMillis();
timerHandler.postDelayed(timerRunnable, 0);
b.setText("stop");
}
}
});
}
}
Upvotes: 2
Reputation: 2674
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//write your code here to be executed after 1 second
}
}, 1000);
Upvotes: 6