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);
}
};
Related
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()) {
In my Android application I want to play sequentially some sound that was divided to parts before. Each part is a .wave file with 2-3 seconds length.
I do perform that successfully, but I have a noticeable delay between those parts.
My code looks now like that -
localMediaPlayer = new MediaPlayer[3];
localMediaPlayer[0] = MediaPlayer.create(this, R.raw.sound_1);
localMediaPlayer[1] = MediaPlayer.create(this, R.raw.sound_2);
localMediaPlayer[2] = MediaPlayer.create(this, R.raw.sound_3);
public void onClick_localBtn(View v){
Toast.makeText(this, "Play Local Sound", Toast.LENGTH_LONG).show();
localMediaPlayer[0].start();
localMediaPlayer[0].setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer mp) {
localMediaPlayer[1].start();
}
});
localMediaPlayer[1].setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer mp) {
localMediaPlayer[2].start();
}
});
}
How can I improve my code that those parts will play smooth and with no delay, like if it were a 1 file sound?
Thanks.
When you start playing your (N)th media player, call prepareAsync() on your (N+1)th media player:
localMediaPlayer[0].start();
localMediaPlayer[1].prepareAsync();
localMediaPlayer[0].setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer mp) {
localMediaPlayer[1].start();
localMediaPlayer[2].prepareAsync();
}
});
localMediaPlayer[1].setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer mp) {
localMediaPlayer[2].start();
}
});
Edit
Based on your update and your comment, perhaps a better way would to be time the playing of the next media player according to the duration of the previous player minus some delta:
private static final int DURATION_DELTA = 1000;
private Handler mHandler = new Handler();
public void playMediaPlayer(final int index) {
if (index >= localMediaPlayer.length || localMediaPlayer[index] == null)
return;
localMediaPlayer[index].start();
final int duration = localMediaPlayer[index].getDuration();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
playMediaPlayer(index + 1);
}
}, duration - DURATION_DELTA);
}
This code will basically play a media player, then schedule playing the next media player approx 1 second before the next media player ends. It's a bit "hacky", but you can play around with it (the value for DURATION_DELTA) until you get the best results.
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 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);