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);
Related
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&¤tvolume<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);
}
};
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 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....
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();
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);