Im working on music player for Android and I need to get static time to start playing music. Acutally command context.player.start() take random time from range <2;20> [ms] but I need to get static time of accomplishing method. My code is below. I mean that time difference between "before call method time" and "just after method accomplished time" need to be static (method should take static time)
public class SpeakerActivity extends AppCompatActivity {
MusicPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speaker);
player = new MusicPlayer(SpeakerActivity.this);
run();
}
private void run() {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
ClientTask ct = new ClientTask(SpeakerActivity.this);
try {
ct.run();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
}
public class ClientTask extends Listener {
ClientTask(Context context) { this.context = context);}
...
//I'm only going to implement this method from Listener.class because I only need to use this one.
public void received(Connection c, Object p){
//Is the received packet the same class as PacketMessage.class?
if(p instanceof PacketMessage){
//Cast it, so we can access the message within.
PacketMessage packet = (PacketMessage) p;
if(packet.message.charAt(0) == 'p') {
String latencyString = packet.message.replace('p','0');
int latency = Integer.parseInt(latencyString);
try {
Thread.sleep(latency);
context.player.start(); <---- THIS LINE TAKE RANDOM AMOUNT OF TIME
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class MusicPlayer {
private MediaPlayer player;
private Context context;
public MusicPlayer(Context context) {
this.context = context;
player = MediaPlayer.create(context, R.raw.zero);
}
public void start() {
player.start();
}
public void stop() {
player.stop();
}
}
Does anybody know whats wrong?