Play next sound without waiting for previous playback to end - java

I am making a roulette game type application, I have 10 rectangles that change color simulating a random choice, In addition to the color change, I want a sound to be played every time the roulette goes through a rectangle.
First a list of possible prizes is created awardList, then the winner is randomly selected positionAwardSelected
I tried to do the following:
final Handler handler = new Handler();
int count = 0;
private void playRoulette(){
List<Award> awardList = new ArrayList<>();
int positionAwardSelected;
// ... List filling and winner selection code (not necessary to include for the question) ...
int numberMovements = ThreadLocalRandom.current().nextInt(20, 60 + 1);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.pin);
final Runnable runnable = new Runnable() {
public void run() {
if (count > numberMovements){
resetColors();
getConstrainLayoutRectangle(positionAwardSelected).setBackground(ContextCompat.getDrawable(Roulette.this, R.drawable.selected_rectangle));
} else {
resetColors();
int randomPosition = ThreadLocalRandom.current().nextInt(0, awardList.size());
getConstrainLayoutRectangle(randomPosition).setBackground(ContextCompat.getDrawable(Roulette.this, R.drawable.selected_rectangle));
}
mp.start();
if (count++ < numberMovements) {//Time it takes to change the rectangle, before the winner 70% of the time it takes 175ms and 30% of the time it takes 250ms, to change to the winner it takes 100ms
handler.postDelayed(this, timeDelay());
} else {
handler.postDelayed(this, 100);
}
}
};
handler.post(runnable);
}
private int timeDelay(){
int a = new Random().nextInt(100);
if (a >= 30){
return 175;
} else {
return 250;
}
}
Color changes, speed and award selection work fine, I have 2 sound issues unfortunately. The first problem is that the sound does not match the color change of the roulette wheel, it seems that the sound is slower, the second problem is that the sound seems to go into an infinite loop since it never finishes playing
I realize that the first problem happens because the duration of the sound is greater than the duration of timeDelay (), when timeDelay > duration of sound, it matches the change of the selected rectangle with sound, but I need timeDelay () to be shorter for a fluid motion.
Alternative 2
When I substitute mp.start(); for MediaPlayer.create(this, R.raw.pin).start(); everything matches perfectly regardless of the duration of timeDelay (), but only the first 5 positions of the wheel, then the following loop occurs: silence of about 4 seconds, the sound is played 5 times and it never ends.
Hope someone can help me, thanks

Related

Java - simple animation of rotation

I am beginner when it comes to java and I have come across with a problem that I haven't found a solution to just yet. The thing is that I have working methods for drawing an letter and also for rotating it - when I set rotation it works the way it should. However I would like to make this slightly more interactive, at school we were given the basic framework for that - I was able to create a button that when you click on it the letter's angle changes and the letter is redrawed correctly. But I would like to make it an animation, for example you click on the button and for 10 seconds (or until you press the button again) the letter will be rotating.
On the internet I found a way to perform an action after certain period of time, and I thought I will use this. I wanted to add an angle and redraw an image, after let's say 1 second, then it would repeat - I thought this would make it look like it is animated. But I was wrong. I tried so many ways to do this, the best thing was that after few seconds that I set I want the animation to go for, it changed an angle and redraw, unfortunately it was the final state and it didn't draw states in between to create an animation. And this latest code doesn't even do that, the program just freezes.
int animation = 0;
int steps = 0;
public void G_draw() {
graphic.clear();
if (animace==1)
{
animation();
}
letter('a', G_Color.G_cBlack, 2, 2);//drawing an letter
}
public void G_mousePressed(G_Button button, int x, int y) {
if (button.equals(G_Button.B_LEFT)&&x>700&&x<750&&y>500&&y<520){
animation=1;
G_draw();
}
}
public void animation() {
long start = System.currentTimeMillis();
long end = start + 2 * 1000;
while (System.currentTimeMillis() < end) {
}
langle+=30; // adding an angle
steps++;
G_repaint();
G_draw();
if (steps<4) animace();
}
instead of this
long start = System.currentTimeMillis();
long end = start + 2 * 1000;
while (System.currentTimeMillis() < end) {
}
use
Thread.sleep(# in milliseconds);
We can't see where you use langle
This line if (steps<4) animace(); only runs when animace<>1 otherwise it's skipped because animate()calls G_draw() calls animate and so on.

Return MouseWheel value as scrolling/not scrolling in Processing every 3 seconds?

I'm very new to Processing and am trying to make a simple program that handles a list of audio clips. When the user scrolls down using the mouse wheel, audio clips are skipped, and when they stop scrolling, an audio clip plays through.
The first step for this seems to be finding a way of converting the numbers returned from MouseWheel, which grow vastly larger or smaller as the mouse wheel is scrolled, into true/false-style values that are returned less frequently (every few seconds).
The code below returns true/false, but does so very quickly. If anybody could help me with saying, "scan for changes every 3 seconds", or similar, I'd be enormously appreciative.
int wheelVal; //reading from mouse wheel
boolean hasScrolled = false; // whether mouse has scrolled
void setup() {
size (750, 500);
println ("ready to scroll");}
void draw() {
background (450);}
void mouseWheel(MouseEvent me)
{ wheelVal = me.getCount();
if (wheelVal > 0) {
hasScrolled = true;
println ("true");}
if (wheelVal < 0) {
hasScrolled = false;
println ("false");}
}
I think you're on the right track, but you can probably actually simplify your logic a bit.
Step 1: Create a hasScrolled variable. Set it to true in the mouseWheel() function.
Step 2: Inside the draw() function, use the millis() function or the frameCount variable to check when 3 seconds has gone by. If so, check the hasScrolled variable, and set it back to false.
Here's a small example:
boolean hasScrolled = false;
void draw(){
background(32);
//180 frames is 3 seconds
if(frameCount % 180 == 0){
if(hasScrolled){
println("scrolled!");
}
}
}
void mouseWheel(MouseEvent me){
hasScrolled = true;
}

How to make my game stop after ten seconds in libgdx?

I'm almost done with my project to make a very simple game. The point of the game is to count how many times the user touches the screen and compare it to the number of times an object popped up on the screen to be touched. It's considered a win if number of touches = number of objects. All of this is to happen in 10 seconds, and I have no idea where to start on how to time the game? (It's supposed to be a veryyy very simple game just to learn how to use libgdx)
As of now, there's a start screen that the user touches to start and then the game starts where objects pop up to be touched. The problem is that it's basically an endless game right now... Here's how I make an object "randomly pop up":
if (Gdx.input.isTouched()) {
touchCount++;
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
visual.x = MathUtils.random(10, 700);
visual.y = MathUtils.random(100, 400);
}
Do I have to put some built in timer function in this part of the code? And how to I go about tracking the touches to compare with the number of objects that popped up?
Thanks, any advice would be very appreciated!
System.currentTimeMillis() gives you current time in milliseconds
For example you can do something like this
private long time;
public void show(){
//...
time = System.currentTimeMillis();
}
public void draw(){
//...
if(System.currentTimeMillis()>time+10000){
System.out.println("after 10 seconds");
}
}
You may use old fashioned java way either:
Timer timer = new java.util.Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
// Pause your game or just close it...
Gdx.app.exit();
}
}, 10000L);
Of course it's better to use synchronous timer approach, but for fast debuging or prototyping needs its sometimes more convenient.
Use the delta value inside your render() method.
render(float delta){
// ......
if (yeni_oyun_mesaji_var_MI)// a boolean value to increment my time value only it is needed.
oyun_uyari_zamani_tutucu += delta;
if (oyun_uyari_zamani_tutucu > oyun_uyari_zaman_siniri) {// after some time (**oyun_uyari_zaman_siniri**) do what you need
oyun_uyari_zamani_tutucu = 0f;
oyun_durum_mesaji_str = "";
yeni_oyun_mesaji_var_MI = false;
Gdx.app.log(TAG, "Uyarı Kapatıldı!");
}
}

when shooting constantly repeats and goes faster every key stroke

Ok so i'm going to try to explain this, well I created a shoot method in a class that contains my bluespell, and all of it's constructors, well the problem is when I press space once it constantly shoots without me pressing it again, and if I press it twice the speed at which it fires doubles and it starts to contain more than one x and y position on my grid I just want the spell to fire when fired and I only need one item because I don't want there to be more than one instance of it on the grid I want it to be that the player cannot fire until the spell has left the grid, here's my code thanks oh and I only have it called in my key released seeing as it should only do it once the key has been released, but if that should change please let me know thanks :)
public void shootSpell(){
final BlueSpell b = new BlueSpell(GoodGuy.getx(), GoodGuy.gety() +1, BlueSpellWizard());
int delay = 100;
ActionListener taskPerformed = new ActionListener(){
public void actionPerformed(ActionEvent e){
if(b.gety() != 19){
WizardCells[b.getx()][b.gety()].setIcon(null);
WizardCells[b.getx()][b.changey(b.gety()+1)].setIcon(b.getIcon());
}
else{
WizardCells[b.getx()][b.gety()].setIcon(null);
b.changex(GoodGuy.getx());
b.changey(GoodGuy.gety() +1);
}
}
};
new Timer(delay, taskPerformed).start();
else if(key == KeyEvent.VK_SPACE){
GoodSpell.shootSpell();
}
Do not use a Timer! Your task should not repeat every 100 milliseconds. If I understand your code, you should run the code from your ActionListener in a new thread.
// Something like this,
new Thread(new Runnable() {
public void run() {
if (b.gety() != 19) {
WizardCells[b.getx()][b.gety()].setIcon(null);
WizardCells[b.getx()][b.changey(b.gety() + 1)].setIcon(b
.getIcon());
} else {
WizardCells[b.getx()][b.gety()].setIcon(null);
b.changex(GoodGuy.getx());
b.changey(GoodGuy.gety() + 1);
}
}
}).start();
You also need to do a check if the spell is currently in view/activate prior to initiating a new spell in the shoot() method...
public void shootSpell(){
//do a check here if a spell is already running!
final BlueSpell b = new BlueSpell(GoodGuy.getx(), GoodGuy.gety() +1, BlueSpellWizard());
int delay = 100;
//......
So in your method that is updating the spell going accross the screen you either need to have a flag in there if its still active, or if you are running it in a new thread, save that thread to a global var and check to see if the thread is running prior instantiating a new BlueSpell()

change image of imagebutton

I'm begining on android and java.
I try to make a simon game but have some problems.
I wrote this to show the simon buttons sequence or the button pushed by the player:
if (but_num == 1) {
ib1.setImageResource(R.drawable.bullet_square_green);
MediaPlayer sound = MediaPlayer.create(this, R.raw.tone_green);
sound.start();
for (int x = 1; x < 10000000; x++) { };
ib1.setImageResource(R.drawable.bullet_ball_green);
} else if (but_num == 2) {
It should change the image of each imagebutton, play a sound, wait some time (for {}) and then
change the image again....
But it doesn't work well... it plays the sound and really changes the image by bullet_square_xxx, but the eye can't see the image change, the change is only visible if the image is not changed back again by the bullet_ball_xxx :-(
I think this is my fault because I wrote the code different than java really works... I'm a
beginner and don't think in java... I have the visual basic program structure on my mind yet.
Thank You and sorry for my English !
This is probably caused by a delay on the event dispatch thread and the fact that the empty loop might be even ignored by the compiler since it is static, it is easily predicted to have no effect on the program. My suggestion is first force a repaint/update on the GUI and use Thread.sleep. Something like this:
if (but_num == 1) {
ib1.setImageResource(R.drawable.bullet_square_green);
updateUI(); // if you are somewhere in a class extending any Frame/Panel
//If you are in other class use mainFrame.repaint();
MediaPlayer sound = MediaPlayer.create(this, R.raw.tone_green);
sound.start();
try{
Trhead.sleep(3000);
} catch (InterruptedException e) {}
ib1.setImageResource(R.drawable.bullet_ball_green);
updateUI(); //only if this effect is delayed too
} else if (but_num == 2) {
ok....I think delay is the problem in your code. Since nowadays there are highspeed processors available that can count to 10000000 in a few ms, mine does. So instead of using the old-school for loop to introduce a delay use
Thread.sleep(5000);
this causes a delay of 5 sec, the argument is the time in milliseconds.
there is another thread which talks about introducing a delay:
How to pause / sleep thread or process in Android?
you could try this [i have copied pasted from that thread]:
if (but_num == 1) {
ib1.setImageResource(R.drawable.bullet_square_green);
MediaPlayer sound = MediaPlayer.create(this, R.raw.tone_green);
sound.start();
// SLEEP 2 SECONDS HERE ...
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
ib1.setImageResource(R.drawable.bullet_ball_green);
}
}, 2000);
} else if (but_num == 2) {

Categories