Updating seekbar stops audio while playing audio - android

I am making an audio player and want to update seek bar while playing audio. I have searched online and written a method to update it using runOnUiThread but it stops the music while updating the seek bar. How to solve it apart from using services.
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
int duration = mp.getCurrentPosition() / 1000;
seekbar.setProgress(duration);
mHandler.postDelayed(this, 1000);
}
});

you should do it like this
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
int duration = mp.getCurrentPosition() / 1000;
seekbar.setProgress(duration);
mHandler.postDelayed(this,1000);
}
},1000);

Try using Timer, example:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
int duration = mp.getCurrentPosition() / 1000;
seekbar.setProgress(duration);
}
}, 1000, 1000);
When you want cancel
timer.cancel();
timer = null;
More info about Timer

The problem is seekBar.setOnSeekBarChangeListener(this); So put it in Your AudioControl() Method..... You dont set oncheckchangedlistner to your seekbar....

Related

Can be use CountDownTimer within a loop?

I want to use CountDownTimer within a for loop but when I am using following code then CountDownTimer is running only once while I want to run it CountDownTimer as per given condition in for loop. it might be a silly question but I will be very thankful to you if I get some help. Thanks in Advance
for (int i=1;i<=10;i++){
Random random = new Random();
totalques.setText(String.valueOf(i) + "/10");
firstnum.setText(String.valueOf(random.nextInt(100)));
secondnum.setText(String.valueOf(random.nextInt(50)));
new CountDownTimer(5000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
time.setText(String.valueOf(millisUntilFinished / 1000)
+"s");
}
#Override
public void onFinish() {
}
}.start();
}
You can use the Timer class for doing your job
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
//one second elapsed
}
});
}
};
timer.schedule(timerTask, 0, 1000);
The schedule method say start directly and I want to be notified when every 1000 milliseconds elapse.
Don't forget to cancel the timer when you want to stop it with timer.cancel()
Inside the run you can decrement an int and when it reach 0 you can stop the timer

How to stop MediaRecorder after certain time in Android?

I want to stop recording a video after a certain time e.g. 5 seconds.
Do you know how to do that with a MediaRecorder?
You can use a handler to step the recording after that time.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
recorder.stop();
}
}, DELAY);
Regarding the timer:
int t = 0;
handler.postDelayed(new Runnable() {
#Override
public void run() {
textView.setText(getString(R.string.formatted_time, t));
if(++t<10) {
handler.postDelayed(this, 1000);
}
}
}, 1000);
Where formatted_time is something like that:
<string android:name="formatted_time">%d seconds</string>
mCountdowntimer=new CountDownTimer(countdownPeriod, 1000) {//countdown Period =5000
public void onTick(long millisUntilFinished) {
textView.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mediaplayer.stop(); }
}.start();
and must not forget to call release(); after calling stop();

I need a simple incrementation over time function

I want to do a cookie clicker like app and i need a simple incrementation over time function.
But i would only want the int to start increasing once i have pressed a button.
I tried this but does not work properly.
int delay = 5000;
int period = 1000;
int count = 0;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
count++;
score.setText(String.valueOf(count));
}
}, delay, period);
The reason its not working is because run() is running on separate Thread, not on UIThread. You need to run setText in UIThread. see the code below
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
count++;
score.setText(String.valueOf(count));
}
});
}
}, delay, period);

Android repeat action inside other action repeating

Sorry for my English! :)
Ok, I want to repeat something multiple times every second - like here:
//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
//Called each time when 1000 milliseconds (1 second) (the period parameter)
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);
Now, inside it I want to generate random number between 1-3 (including) and do something if it is 3.
So:
//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
Random rand = new Random();
int num = rand.nextInt(3)+1;
if(num==3){
// repeat action here.
}
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);
And inside the if statement, I want to repeat other action (moving ImageView every 5 milliseconds or something like this). How can I do it? Thank you.
You can use either a CountDownTimer
http://developer.android.com/reference/android/os/CountDownTimer.html
e.g. create a CountDownTimer for 30secs (30000ms) and notify each second (1000ms)
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//I get called every 1000ms
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
or just a Handler.
http://developer.android.com/reference/android/os/Handler.html
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
//dostuff
handler.postDelayed(this,1000); //repeat after a second
}
});
For animations you should have a look at ObjectAnimator/ViewPropertyAnimation.

I want to play Media Player after 10 seconds

I want to play media player after 10 seconds time delay in android.
player = MediaPlayer.create(getApplicationContext(), R.raw.sleep);
player.start();
You can try something like below: The trick is to start the player after 10 seconds by using a CountDownTimer
player = MediaPlayer.create(getApplicationContext(), R.raw.beepsound);
CountDownTimer timer = new CountDownTimer(10000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
player.start();
}
};
timer.start();
Don't forget to stop & release the player when done with it.
Another approach is with Handler
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run(){
player.start();
}
}, 10 * 1000);

Categories

Resources