updating text with timer and mediaplayer in android - android

hi guys i want to update my text while my mediaplayer is playing
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
running.setProgress(mediaPlayer.getCurrentPosition());
TextOfMaxValue.setText(mediaPlayer.getDuration());
}
}, 0, 1000);
and when i use settext inside my timer like my code above my app crashes and do not start
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
running.setProgress(mediaPlayer.getCurrentPosition());
}
}, 0, 1000);
it works well with the code above but i want to update my text too .

You need to pass a String argument to your TextView, not an int.
Use:
String durationText = String.valueOf(mediaPlayer.getDuration());
TextOfMaxValue.setText(durationText);

Related

Why isn't my text string updating?

So I am trying to display the time passed since pressing a button on my app.
My code is:
/*This will initiate the timer*/
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
start=System.currentTimeMillis();
time=System.currentTimeMillis()-start;
currenttimedisplay.setText(Long.toString(time));
}
});
}
}, 0, 1000);
The app runs but when I press the button it just shows "0.0".
The app doesn't close out. Any help is appreciated!
Please try this:
/*This will initiate the timer*/
timer = new Timer();
start=System.currentTimeMillis();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
time=System.currentTimeMillis()-start;
currenttimedisplay.setText(Long.toString(time));
}
});
}
}, 0, 1000);
In your code, the value of start is changed everytime the timer elapsed (start = System.currentTimeMillis()), so the value of time is always 0 (System.currentTimeMillis() - System.currentTimeMillis() should be 0 if it is called with no, or very small time difference...). So you should set the value of start on button press, then calculate the difference, and update text view in your timer task.

Timer not working properly in android

Any one give idea, Timer will be schedule based on duration array and if once complete duration array length again will be start from first. In this some loop working perfectly some times will be crash and through arrayoutofboundsexception. Any one cane Help Me???
int[] duration={10000,2000,8000};
int layoutIncrment=-1;
private void layoutRotate()
{ layoutIncrment++;
Timer timer = new Timer();
timer.schedule(new TimerTask()
{ public void run()
{
if((duration.length-1) <= layoutIncrment)
layoutIncrment=-1;
layoutRotate();
Log.i("Rotation",String.valueOf(layoutIncrment));
} },duration[layoutIncrment],100000);
}
why dont u try like this. it will keep running in the while loop.. thats only u want. right?
int n=1;
while(n>0)
{
for(int i=0;i<duration.length;i++)
{
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//add ur code here
}
}, duration[i]);
}
}

How to run code in TimerTask regualarly

I use this code to run my code regularly ,
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//my code here
}
});
}
}, 0, 50);
There is a parameter of timer receives Date object to run code in specifec date, But
I need to Run my code with timertask every friday of week,
Any way?
Afakomoallah, Best regards.

Displaying a stopwatch in an android app in a textView

I have setup a stop watch using the com.apache.commons library and the stop watch seems to work fine. What I don't know how to do is display this stopwatch in a textView in my app. In general, I have no idea how that would work, i.e. How exactly would a stopwatch be displayed in a textView, given that the time on a stopwatch keeps changing constantly? At the moment, I have the code below and it updated the text in the textView every second for about 2 seconds and then I got a weird error. I'm not even sure if this is the right way to go about doing this. Please help!
Timer timer = new Timer();
TimerTask timerTask;
timerTask = new TimerTask() {
#Override
public void run() {
timeText.setText(time.toString());
}
};
timer.schedule(timerTask, 0, 1000);
The error I got after 2 seconds (and it successfully updated the time) was :
"only the original thread that created a view hierarchy can touch its views"
You can only update a TextView on the UI thread.
runOnUiThread(new Runnable() {
#Override
public void run() {
//stuff that updates ui
}
});
Your code becomes
Timer timer = new Timer();
TimerTask timerTask;
timerTask = new TimerTask()
{
#Override
public void run()
{
runOnUiThread(new Runnable()
{
#Override
public void run()
{
timeText.setText(time.toString());
}
});
}
};
timer.schedule(timerTask, 0, 1000);
You may have to do myActivityObject.runOnUiThread() if you're getting an error there.
See this for more detail.
To update a view from another thread, you should use handler.
private void startTimerThread() {
Handler handler = new Handler();
Runnable runnable = new Runnable() {
private long startTime = System.currentTimeMillis();
public void run() {
//Change the condition for while loop depending on your program logic
while (true) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable(){
public void run() {
timeText.setText(time.toString());
}
});
}
}
};
new Thread(runnable).start();
}

Image view image need to change periodically in android

I have an image view. So in the image view i want to change the images after a certain time period. Images are coming from an array list. Now when the no of images are 3 or more than 3, it is working perfect. But when it is 2, my logic is not working. Second image is visible for a moment and then again changed to first image here is my code:
r = new Runnable(){
int i = 0;
public void run(){
iv.setImageBitmap(alBmps.get(i));
i++;
if(i >= alBmps.size()){
i = 0;
}
iv.postDelayed(r, 5000); //set to go off again in 5 seconds.
}
};
iv.postDelayed(r, 1000);
Can any one help me what changes i need on the above code?
Thanks.
Try this
declare variables
static int i=0;
private Timer myTimer;
in your onCreate or on button click where you want to call and start the methods
myTimer = new Timer();
myTimer.schedule(new TimerTask()
{
#Override
public void run()
{
TimerMethod();
}
}, 500, 5000);
add these methods to your class
private void TimerMethod()
{
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable()
{
public void run()
{
if(i<alBmps.size())
{
iv.setImageBitmap(alBmps.get(i));
}
else
{
i=0;
iv.setImageBitmap(alBmps.get(i));
}
i++;
}
};

Categories

Resources