Sound during countdown timer between two numbers - android

I am making an app where a different sound needs to be played based on the number the countdown timer is on.
// Define CountDown Timer Attributes//
waitTimer1 = new CountDownTimer(60000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
long timeLeft = millisUntilFinished / 1000;
Timer.setText("" + String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
if (timeLeft >= 43) {
mp.start();
}
}
#Override
public void onFinish() {
t1.setEnabled(true);
t2.setEnabled(true);
next.setEnabled(false);
Timer.setText("0:00");
next.setText("Start");
waitTimer1 = null;
}
}.start();
So far I have it so a sound plays from 1:00 to :43 seconds like so:
if (timeLeft >= 43) {
mp.start();
}
I need the next sound to play from 42 seconds to 27 seconds something like this:
if (timeLeft <42,>27) {
mp2.start();
}
Obviously this doesn't work I feel like i'm just typing it in wrong. I need the app to know to start the second sound when the timer hits 43 seconds then stop at 27 seconds. Thanks

You are looking for this:
else if(timeLeft <= 42 && timeLeft > 27)
{
mp2.start();
}
This will loop from 42 through 28 and thus once it ticks to 27 it will stop.
For every condition inside an if statement you need to explicitly tell the compiler what two variables you are comparing. In this case we have 2 conditions:
timeLeft <= 42 and timeLeft > 27
The comparsion between statements can either be Logical Or or Logical And
Logical Or is represented by ||
Logical And is represented by &&
In your case you want to use the logical and statement to compare your two conditions.

Related

MediaPlayer doesn't start when CountDownTimer is finished

I have a CountDownTimer like this:
cuentaAtras = new CountDownTimer(mili, 1000) {
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished / 1000;
if (seconds <= TIEMPO3){
if (tic1.isPlaying()){
tic1.stop();
tic2.start();
}
} else if (seconds <= TIEMPO && seconds > TIEMPO2 + 10){
tic1.start();
}
}
public void onFinish() {
if (tic2.isPlaying())
{
tic2.stop();
}
ticExp.start();
tic1.reset();
tic2.reset();
ticExp.reset();
}
}.start();
Variables tic1, tic2 y ticExp type is MediaPlayer :
tic1 = MediaPlayer.create(getApplicationContext(), R.raw.tic1);
tic1.setLooping(true);
tic2 = MediaPlayer.create(getApplicationContext(), R.raw.tic3);
tic2.setLooping(true);
ticExp = MediaPlayer.create(getApplicationContext(), R.raw.exp);
When I execute onFinish method, the MediaPlayer ticExp should be played, but this doesn't occur.
I tested write ticExp.start() in method onTick and this works, so the file is not corrupted or something like this.
I don't have idea why in onTick method works, but in onFinish no.
(But the others sentences of code in onFinish are executed without problems)
Definitively, I want that ticExp will be played in onFinish method.
Sorry for my English. Thanks.
You are doing ticExp.reset(); after ticExp.start(); in onFinish(), That means you are actually starting and also resetting(making it idle). Just remove ticExp.reset(); in onFinish().
For the Complete State of MediaPlayer, you can refer this
And one more thing, Your else condition in onTick method will never execute.

How can I control the volume of the MediaPlayer by programming?

I am using MediaPlayer to play a music in 60 seconds. I want to increase the sound of the music when it is playing follows the rule:
1. First 10 seconds, the volume is 20% of maximum volume
2. From 20 seconds to 30 seconds, the volume is 40% of maximum volume
3. From 30 seconds to 40 seconds, the volume is 80% of maximum volume
3. From 40 seconds to 60 seconds, the volume is 100% of maximum volume
How can I control the volume of the MediaPlayer by programming in Android M?. This is my code to play music. It is missing the task control volume. Thank all
public MediaPlayer mpCalling = null;
private CountDownTimer mCountDownTimer_Playing;
public void playSound(){
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
try {
if (mpCalling != null && mpCalling.isPlaying()) {
mpCalling.stop();
mpCalling.release();
mpCalling=null;
}
mpCalling = MediaPlayer.create(getApplicationContext(), notification);
mpCalling.start();
if(mCountDownTimer_Playing!=null){
mCountDownTimer_Playing.cancel();
mCountDownTimer_Playing=null;
}
mCountDownTimer_Playing = new CountDownTimer(60000,1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
mpCalling.stop();
}
};
mCountDownTimer_Playing.start();
} catch (Exception e) {
e.printStackTrace();
}
}

Android play beep sound, every n milliseconds

I created an Android application that is like a metronome.
Actually I want to play a beep sound every n milliseconds.
I use MediaPlayer and timer for this.
My code is like this:
Soloution 1:
start_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Timer timer = new Timer("MetronomeTimer", true);
TimerTask tone = new TimerTask() {
#Override
public void run() {
//Log
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss:SS");
Date date = new Date();
j++;
Log.i("Beep", df.format(date) + "_____" + j);
//Play sound
music = MediaPlayer.create(MainActivity.this, R.raw.beep);
music.start();
}
};
timer.scheduleAtFixedRate(tone, 500, 500); // every 500 ms
}
});
When I run this code, everything is OK. But after 15 times loop, line of Log works fine, but sound is muted. And occasionally every 15, or 20 Log, sound plays and stop.
Solution 2:
I move this line:
music = MediaPlayer.create(MainActivity.this, R.raw.beep);
out of TimerTask (like this):
start_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
music = MediaPlayer.create(MainActivity.this, R.raw.beep);
Timer timer = new Timer("MetronomeTimer", true);
TimerTask tone = new TimerTask() {
#Override
public void run() {
//Log
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss:SS");
Date date = new Date();
j++;
Log.i("Beep", df.format(date) + "_____" + j);
//Play sound
music.start();
}
};
timer.scheduleAtFixedRate(tone, 500, 500); // every 500 ms
}
});
This code is OK too until 500 ms for repeating. when period time decrease to 400 or 300 ms, every 2 Log, one sound beeps.
How to repair this code that work fine.
SoundPool is better for playing short sounds that are loaded from memory. I have also had problems implementing MediaPlayer, SoundPool has just been a much easier and lower latency experience.
If you set .setMaxStreams() to a number above 1 you shouldn't get any muted sounds. Try and experiment.

Playing a video between two times in Android

I need for an android app the ability to play a video from a time t1 to an other time t2. Basically, this video is a kind of "sprite" in my app (it's an .mp4 video h264 baseline encoded).
So, I tried the following. A button "next" plays the video between t1 and t2. To check if the video has arrived to t2, I use an handler with a postDelayed call every 20 ms. If the video current position is bigger than t2, I stop the video. Otherwise, I check again in 20ms.
But it does not work. For a reason I don't understand, the video current position I read in postDelayed goes suddenly from a time to a few seconds later. On my screen, the video plays normally.
Some code:
// MainActivity
// setting up the video
vidView = (VideoView) findViewById(R.id.myVid);
vidView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/raw/myvid"));
// wiring up Bt next
Button nextBt = (Button) findViewById(R.id.next);
nextBt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playVideo();
}
});
And the playVideo method:
private void playVideo() {
// t1 and t2 are defined within MainActivity class
t1 = (int) (Math.random() * (80000));
t2 = t1 + (int) (Math.random() * (5000));
// we start at t1
vidView.seekTo(t1);
// We check video position in 20 ms
mHandler.postDelayed(new Runnable() {
public void run() {
int currentTime = vidView.getCurrentPosition();
Log.i("currentTime", String.valueOf(currentTime));
Log.i("T2", String.valueOf(t2));
// Ok, we can pause the video
if(currentTime > t2 - 20) {
vidView.pause();
}
// We check again in 20ms
else {
mHandler.postDelayed(this, 20);
}
}
}, 20);
vidView.start();
}
The logCat gives me (in this example, T1 = 47286 and T2 = 51478)
10-14 11:31:56.603: I/currentTime(3359): 47286 // first call to postDelayed, fine
10-14 11:31:56.603: I/T2(3359): 51478
10-14 11:31:56.623: I/currentTime(3359): 47286 // second call to postDelayed, still ok
10-14 11:31:56.623: I/T2(3359): 51478
10-14 11:31:56.653: I/currentTime(3359): 50000 // What? How the video current time can already be 3 seconds later?
10-14 11:31:56.653: I/T2(3359): 51478
Can you tell me what's wrong with my code?
Thanks!
Instead of constantly checking why don't you just set a handler to stop it at a specific later time? You don't need to check if it has "arrived" at T2 - you know when you want to stop it, so just schedule to stop it then.
Runnable stopVideoTask = new Runnable(){
#Override
public void run() {
vidView.pause();
}
};
int t2 = (int) (Math.random() * 5000);
vidView.seekTo(t1);
vidView.start();
mHandler.postDelayed(stopVideoTask, t2);
The video will stop after Math.random() * 5000 seconds of playing.

Increase the sound in each 5 seconds through Media Player

I want to increase the sound by one level up in each and every 5 seconds meanwhile the sound clip plays.Below is my code:-
MediaPlayer player;
player=MediaPlayer.create(this, R.raw.alarm);
player.setLooping(true);
Is there any method in which i can track each and every 5 minutes when over.How can i achieve this?
I think you can achieve this with java TimerTask, this is an example code, not tested but should work with no big modifications :
Basically you start a task every 5 seconds, in the run() function of the TimerTask you level up your sound level, and when you reach the maxSoundLevel you call cancel() to stop the task.
//Put this in global
int REFRESH_INTERVAL = 5 * 1000; //5 seconds
int maxSoundLevel = 10; // Number of loop to get to max level
int curSoundLevel = 0; //Start at 0 volume level
//Put this after you started the sound
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTimerTask(), 0, REFRESH_INTERVAL);
//Put this after your method
private class MyTimerTask extends TimerTask{
public void run() {
if(curSoundLevel < maxSoundLevel)
{
float logLevel = (float)(Math.log(maxSoundLevel-curSoundLevel)/Math.log(maxSoundLevel));
yourMediaPlayer.setVolume(1-logLevel);
curSoundLevel ++;
}
else {
this.cancel();
}
}
}
If you have more questions feel free to ask me.

Categories

Resources