我正在为Android播放音乐播放器,我需要获得静态时间才能开始播放音乐。 acutally命令context.player.start()从范围< 2; 20>获取随机时间。 [ms]但我需要获得完成方法的静态时间。我的代码如下。我的意思是在呼叫方法时间和#34之间的时间差。并且"在方法完成时间之后"需要是静态的(方法应该采取静态时间)
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();
}
}
有人知道什么是错的吗?