Metronom app - problem with threading
Hello,
I have some kind of problem with my metromom application made in j2me. So, without threading this application have sometimes slowdowns, ofcourse this is not acceptable. So I decided to make some threading in this application, but I done something wrong, because application play "Tick" only once. And then is Silence, without threading application was working fine. Here is my source code:
fragment of class "Designer" where i start timer:
Code:
protected void startMetronome() {
calculateInterval = 60000/(howManyBpm);
startTimerEvents = new MetronomeTimerEvent(metrum);
timer.schedule(startTimerEvents, 1000, calculateInterval);
}
class "MetronomeTimerEvent" where i specify a new thread and some kind of things.
Code:
import java.util.TimerTask;
/**
* @author Damian
*/
public class MetronomeTimerEvent extends TimerTask{
public MetronomeTimerEvent(byte metr) {
i = 0;
metrum = metr;
playThis = new PlayTik();
playThisStart = new Thread(playThis);
}
public void run() {
playThis.changeVariableI(i);
playThisStart.start();
i++;
if(i==metrum)
i=0;
}
private byte i;
private PlayTik playThis;
private Thread playThisStart;
public byte metrum;
}
Class "PlayTik" where I play a sounds.
Code:
import javax.microedition.media.control.*;
import javax.microedition.media.*;
/**
* @author Damian
*/
public class PlayTik implements Runnable {
public PlayTik() {
try {
playTick = Manager.createPlayer(getClass().getResourceAsStream("/1.wav"), "audio/x-wav");
playTick.realize();
playTick.prefetch();
} catch(Exception ex) { }
VolumeControl volumeV = (VolumeControl)playTick.getControl("VolumeControl");
volumeV.setLevel(99);
try {
playTock = Manager.createPlayer(getClass().getResourceAsStream("/2.wav"), "audio/x-wav");
playTock.realize();
playTock.prefetch();
} catch(Exception ex) { }
VolumeControl volumeV1 = (VolumeControl)playTock.getControl("VolumeControl");
volumeV1.setLevel(99);
}
public void run() {
testPlay();
}
public int testPlay() {
try {
if(i==0) {
playTock.start();
return 0;
}
else {
playTick.start();
return 0;
}
}catch(Exception ex) { return 1; }
}
public void changeVariableI(byte changedVariable) {
i = changedVariable;
}
public byte i;
private Player playTick;
private Player playTock;
}
Hope you help me find a solution of this problem! : )