0

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?

Jo Dung
  • 162
  • 2
  • 18
  • 1
    What is static time? What do you want, and what do you get instead? Reading your title it's totally unclear what you want. Do you want add a 0-20 ms delay? Or do you have a delay and you want to get rid of it? Please update the question – Gavriel Feb 08 '16 at 15:11
  • Okay, I just updated. Is it clear now? – Jo Dung Feb 08 '16 at 15:16
  • you mean constant time? Like it always executes in the same amount of time? Or do you want it to execute faster so that it is more responsive? – rothloup Feb 08 '16 at 15:46
  • Yes, I mean constant time. Its not nessesary to execute it faster or slower. I just need to get constant time of execution – Jo Dung Feb 08 '16 at 17:04
  • I can't find a member or method called `player` in the class `Context`, so I'm surprised that line executes at all (or even compiles). Where is `player` defined as a member of `Context`? – rothloup Feb 08 '16 at 18:44

0 Answers0