How to stop countDownTimer when a variable reaches a certain value - android

What I am trying to do basically is that I want my countDownTimer to run 60 times , i want to do this by setting the value of a variable to 0 and stopping the timer when the variiable reaches the value of 60.
i=0;
new CountDownTimer(3600000,60000) {
public void onTick(long millisUntilFinished) {
imageClock.setImageResource(image1stRec[i++]);
}
public void onFinish() {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
mp.start();
}
}.start();

Below method help you to shecdule a countdown for 60 times
new CountDownTimer(60000,1000) {
public void onTick(long millisUntilFinished) {
imageClock.setImageResource(image1stRec[i++]);
}
public void onFinish() {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
mp.start();
}
}.start();

Related

Increasing the volume of media player while playing

i want to make a special alarm app and the user should have the possibility to be woken up with music getting louder e.g. for 60 seconds.
I could not find a way to do this, that's why I need your help.
Thank you for your help and sorry for my bad English
final AudioManager am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
oldvolume=am.getStreamVolume(AudioManager.STREAM_MUSIC);
am.setStreamVolume(AudioManager.STREAM_MUSIC,100,0);
if (increase<=0) mediaPlayer.setVolume(volume,volume);
mediaPlayer.start();
if (increase>0){
mediaPlayer.setVolume(0,0);
final double hohe=volume/increase;
new CountDownTimer((increase*1000),1000) {
#Override
public void onTick(long millisUntilFinished) {
mediaPlayer.pause();
mediaPlayer.setVolume((float) hohe*millisUntilFinished,(float) hohe*(increase*1000-millisUntilFinished));
mediaPlayer.start();
}
#Override
public void onFinish() {
mediaPlayer.pause();
mediaPlayer.setVolume( volume,volume);
mediaPlayer.start();
}
}.start();
That's what i got until now.
increase= increasing time in seconds
volume= max volume alarm should have
Solution, works for me is:
private Handler mHandler = new Handler();
private Runnable mVolumeRunnable = new Runnable() {
#Override
public void run() {
if (mediaPlayer != null&&currentvolume<endvolume) {
currentvolume += volumeincrease;
mediaPlayer.setVolume(currentvolume/100f, currentvolume/100f);
Toast.makeText(AlertActivity.this,String.valueOf(currentvolume),Toast.LENGTH_SHORT).show();
mHandler.postDelayed(mVolumeRunnable, 1000);
}
else mHandler.removeCallbacks(mVolumeRunnable);
}
};

I'm trying to make an android app where an alarm plays when the battery gets under 10%. Why isn't my code working?

The audio file (.mp3) gets played when I don't use the onPreparedListener, but I get an error after the file tries to replay after some time (because it's not listening for the state obviously). So basically the code is checking the battery state and if it's 10% or below it plays the alarm sound, I also have a button which I can click to stop the sound. Now with the onPreparedListener the alarm sound doesn't get played anymore. What am I doing wrong?
tv_battery = (TextView) findViewById(R.id.tv_battery);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.alarm);
Button b = (Button) findViewById(R.id.stop_alarm);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying()) {
mp.stop();
mp.release();
}
}
});
runnable = new Runnable() {
#Override
public void run() {
int level = (int) batteryLevel();
tv_battery.setText("Battery: " + level + "%");
handler.postDelayed(runnable, 5000);
if(level <= 10) {
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mp) {
CountDownTimer count = new CountDownTimer(7000, 1000) {
public void onTick(long millisUntilFinished) {
mp.start();
}
public void onFinish() {
//code fire after finish
mp.stop();
}
};
count.start();
}
I think you have place mp.start(); on wrong place please move it before count.start(); as below :
CountDownTimer count = new CountDownTimer(7000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//code fire after finish
mp.stop();
}
};
mp.start();
count.start();
Also correct below condition if media player already started when battery is under 10 otherwise media player again prepared even after started :
if(level <= 10 && !mp.isPlaying()) {

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);

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);

Playing audio file for specific time

Guys i able to play audio file using Mediaplayer. But i need to play a audio file for specific time.
Eg: play audio1.mp3 till 5 minutes.
The audio file is of 10 seconds only.But it should keep playing till 5 minutes.
try this
mediaplayerplayer.setLooping(true);<--- this lets the audio to loop...
and this is the countdown timer
MyCount counter;
Long s1;
counter= new MyCount(300000,1000);
counter.start();
public void asdf(View v){ <---- method for onclick of buttons pause and resuming timer
switch(v.getId()){
case R.id.button1:<-- for pause
counter.cancel();
break;
case R.id.button2:<--- for resume
counter= new MyCount(s1,1000);
counter.start();
}
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
mediaplayer.stop();
mediaplayer.release();
}
#Override
public void onTick(long millisUntilFinished) {
s1=millisUntilFinished;
}
}
this helps you to pause the mediaplayer also... along with timer.. and lets you play the song for complete 5 mins
//decleration
MediaPlayer mp;
//specify the path of media file
String filepath="?";
//onCreate
mp=new MediaPlayer();
Uri myUri = Uri.parse(filepath);
mp.setLooping(true);
mp = MediaPlayer.create(ActivityName.this, myUri);
mp.start();
new CountDownTimer(300000,1000) {
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
if(mp.isPlaying())
{
mp.stop();
}
}
}.start();

Categories

Resources