0

I'm newbie in android programming..

I have a media player in my activity to play a sound.

I want to do some code on a special time that the media player is playing. I mean I want to do code1 during media player is in 0 to 5sec, and do code2 during 5 to 14 and do code3 during 14 to 18sec.

here's my timer code and I don't know how to run my codes on special times..

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView timerTextView;
long startTime = 0;

//runs without a timer by reposting this handler at the end of the runnable
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {

    @Override
    public void run() {
        long millis = System.currentTimeMillis() - startTime;
        int seconds = (int) (millis / 1000);
        int minutes = seconds / 60;
        seconds = seconds % 60;

        timerTextView.setText(String.format("%d:%02d", minutes, seconds));

        timerHandler.postDelayed(this, 500);
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    timerTextView = (TextView) findViewById(R.id.text);

    Button b = (Button) findViewById(R.id.button1);
    b.setText("start");
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Button b = (Button) v;
            if (b.getText().equals("stop")) {
                timerHandler.removeCallbacks(timerRunnable);
                b.setText("start");
            } else {
                startTime = System.currentTimeMillis();
                timerHandler.postDelayed(timerRunnable, 0);
                b.setText("stop");
            }
        }
    });
}

@Override
public void onPause() {
    super.onPause();
    timerHandler.removeCallbacks(timerRunnable);
    Button b = (Button)findViewById(R.id.button1);
    b.setText("start");
}

}
  • please show the code you tried – meda Apr 01 '14 at 21:30
  • Try implementing a timer (see [this answer](http://stackoverflow.com/questions/4597690/android-timer-how) for details). You can start the timer when you start playing the sound, and use timer's callbacks to execute the methods you need for each time on the track. – FMontano Apr 01 '14 at 22:33

1 Answers1

1

This isn't the most elegant solution, especially if all your time blocks will be exactly 5 seconds apart but if you set initalTime when the media player starts, it should work. Further documentation for uptimeMillis() and other Android timer methods are linked here

int initialTime= SystemTimer.uptimeMillis(); 
while (mediaIsRunning){
    int currentTime=SystemTimer.uptimeMillis();
    int elapsed=currentTime-initialTime;
    if (elapsed<=5000){ // 5 seconds
        //your code 1
    }
    else if (elapsed<=10000){ //10 seconds
        //code 2
    }
    //etc ...
}