How to stop MediaRecorder after certain time in Android? - 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();

Related

Android TextToSpeach player didn't play sound

Hi i have a button from which i call the given methods the problem is when i click button for the first time TextToSpeach Player doesn't play the sound but handler code execute perfectly.If i click on button again everything work perfectly means TextToSpeach play the sound.Thank you..
public void selectDest() {
TextToSpeechPlayer.playSound("hello");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Log.d(TAG, "After 1 sec ");
}
},1000);
}
Try this
TextToSpeechPlayer.playSound("hello");
handler = new Handler();
runnable = new Runnable() {
public void run() {
//do your other task here
handler.postDelayed(this, 60 * 1000);
}
};
handler.postDelayed(runnable, 0);
public void selectDest() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
TextToSpeechPlayer.playSound("hello");
Log.d(TAG, "After 1 sec ");
}
},1000);
}

Sequence of actions with delays android

I'm trying to get my app to display a sequence of images, 1 second after the other. Currently my java looks like this:
arrow1.setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
arrow1.setVisibility(View.INVISIBLE);
arrow2.setVisibility(View.VISIBLE);
}
}, 1000);
handler.postDelayed(new Runnable() {
public void run() {
arrow2.setVisibility(View.INVISIBLE);
arrow3.setVisibility(View.VISIBLE);
}
}, 1000);
I'm not getting any errors, but it's also not working as I intended. Arrow2 is not displaying at all, the app is going straight from arrow1 to arrow3 with a slight delay. Is my second handler.postDelayed(new Runnable() function being overriden? How should I best go about having a delay in this scenario?
You can try like this,
private static final int TotalLoopCount = 2;
private int count = 0;
private int mCurrentLoopCount = 0;
Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Your code
}
#Override
protected void onResume() {
super.onResume();
handler.postDelayed(runnable, 0);
}
#Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(runnable);
}
Runnable runnable = new Runnable() {
#Override
public void run() {
arrow1.setVisibility(View.INVISIBLE);
arrow2.setVisibility(View.INVISIBLE);
arrow3.setVisibility(View.INVISIBLE);
if(count == 0) {
arrow1.setVisibility(View.VISIBLE);
} else if(count == 1) {
arrow2.setVisibility(View.VISIBLE);
} else {
arrow3.setVisibility(View.VISIBLE);
}
count++;
if(count == 3) {
count = 0;
mCurrentLoopCount++;
}
if(mCurrentLoopCount < TotalLoopCount) {
handler.postDelayed(runnable, 3000);
}
}
};
You can also use CountDownTimer as below. See official doc for more details.
Set millisInFuture to countDownInterval*3 for 3 images and set countDownInterval for delay between images.
long countDownInterval = 1000; // 1sec interval
long millisInFuture = countDownInterval*10; // 10sec total time
new CountDownTimer(millisInFuture, countDownInterval) {
public void onTick(long millisUntilFinished) {
arrow1.setVisibility(millisUntilFinished < millisInFuture ? View.VISIBLE:View.INVISIBLE);
arrow2.setVisibility(millisUntilFinished > 0 ? View.VISIBLE:View.INVISIBLE);
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

Updating seekbar stops audio while playing audio

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....

Timer switch android TimerTask

b= new TimerTask()
{
#Override
public void run()
{
out_string = hi.getWebPage("http://daniandroid.honor.es/getAllCustomers.php");
frum_timer = Integer.parseInt(out_string.replaceAll("[\\D]",""));
};
};
t.scheduleAtFixedRate(b, 500, 1000);
Every 1000 ms, run() will store the value to int frum_timer
I'm using this for an flashlight application.
When I open the app, I want my app to switch on and off flashlights using frum_timer for interval time.
So if
frum_timer = 1000;
my flashlight will turn on and off every 1 sec.
and if I edit to
frum_timer = 300;
it will turn on and off every 0.3 sec.
I tried instantiate a new TimerTask()
c=new TimerTask()
{
#Override
public void run()
{
timetask = new TimerTask()
{
boolean checking = false;
#Override
public void run()
{
runOnUiThread(new Runnable()
{
#Override
public void run()
{
if(checking==true)
{
//camera turning on
checking=false;
}else{
//camera turninf off
checking=true;
}
}
});
}
};
}
};
//some code
t.scheduleAtFixedRate(c, 50, 700);
but it's quite hard, because when I change the value, a new timer will start and the old one will remain active.
BTW: I know that the code for turning on/off will not change at the same interval as I want because of my amateur programmer skills (if checking...etc)
I have found a way.
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

How to set Media player Duration in android

i want to play MediaPlayer for 1 second. How to set Duration in this code..
player = MediaPlayer.create(getApplicationContext(), R.raw.beepsound);
player.start();
CountDownTimer Timer = new CountDownTimer(1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
player.start();
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
player.stop();
}
};
Timer.start();
You don't need to do anything on the onTick method.
Try this code:
player = MediaPlayer.create(getApplicationContext(), R.raw.beepsound);
player.start();
CountDownTimer timer = new CountDownTimer(1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
// Nothing to do
}
#Override
public void onFinish() {
if (player.isPlaying()) {
player.stop();
player.release();
}
}
};
timer.start();
If you look at the constructor:
public CountDownTimer (long millisInFuture, long countDownInterval)
millisInFuture - The number of millis in the future from the call to start() until the countdown is done and onFinish() is called.
So onFinish() will be called after 1 second (1000 millisecond = 1second).
Ref: http://developer.android.com/reference/android/os/CountDownTimer.html#CountDownTimer(long, long)
You could use TimerTask to schedule a MediaPlayer.stop() to run after 1 secs.
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
MediaPlayer.stop()
}
};
timer.schedule(doAsynchronousTask, 0, 1000); //execute in every 20000 ms
}
can u try this one
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
}
}, timeout);

Categories

Resources