saint
saint

Reputation: 1

How to play a sound a set number of times?

I'm developing an application to do the following:

When you pressed a button, the application plays a sound lasting two seconds, five times. After playing the sound for the fifth time the application sleeps for thirty seconds. After sleeping, the cycle is repeated two more times.

My problem is that I can not stop the sound after it plays for the fifth time. I think I need to use the for loop or a while loop, but I cannot program them correctly.

The loop:

for(i=0; i==5; ) {
    sound.start(); 
    if(sound.isPlaying()) {
        if(false) { 
            sound.stop(); 
            i++; 
        } 
    }
}

sound.stop();

well, now I built this code, but his only fault is the 'for' not working properly ... he is not repeat the code that is inside of him twice. someone can tell me why?

              for( counter=0; counter<2; ++counter){

                new CountDownTimer(20000, 2000) {       
                    public void onTick(long millisUntilFinished) {
                    //call to my UI thread every 2 seconds  
                     sound.start();    
                   }

                   public void onFinish() {          
                   //final call to my UI thread after 10 seconds

                       chrono2.setBase(SystemClock.elapsedRealtime());
                       chrono2.start();

                       chrono2.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {

                           public void onChronometerTick(Chronometer chronometer) {
                               // TODO Auto-generated method stub
                               long elapsedTime = SystemClock.elapsedRealtime()-chronometer.getBase();
                               Log.i(this.getClass().getName(), "" + elapsedTime);

                               if (elapsedTime > 10000){
                                   chrono2.stop();
                                   chrono2.setBase(SystemClock.elapsedRealtime());
                                   Toast.makeText(getApplicationContext(), "" , Toast.LENGTH_SHORT).show();
                               }
                           }
                       });

                    } // end onFinish   
                }.start(); // end count down
            }

Upvotes: 0

Views: 4332

Answers (4)

Mehrdad Faraji
Mehrdad Faraji

Reputation: 3914

this is my solution:
you can use seekTo(0) function then mp start again!

private void startCountDownTimer() {
    cdTimer = new CountDownTimer(total, 1000) {
        public void onTick(long l) {

            mp.seekTo(0);
            mp.start();
}


        public void onFinish() {
            mp.stop();
            MediaPlayer doneMp = MediaPlayer.create(getApplicationContext(), R.raw.finished_challenge);
            doneMp.start();


        }
    }.start();
}

Upvotes: 0

Lumis
Lumis

Reputation: 21629

That is easy all you need is to use a count down timer:

new CountDownTimer(10000, 2000) {       
    public void onTick(long millisUntilFinished) {          
    //call to my UI thread every 2 seconds  
     playSound();    
   }       
   public void onFinish() {          
   //final call to my UI thread after 10 seconds     
    }   
}.start();

And use a simple timer to sleep 30 seconds:

new Handler().postDelayed(new Runnable() {         
  @Override         
  public void run() {             
   //do after 30 seconds              
  }     
 }, 30000);

By adjusting and combining these two you can make your effect...

EDIT: a solution (untested)

int repeatAlarm = 3; //above onCreate
....

playSoundFiveTimes(); //in onCreate


.......

private void playSoundFiveTimes(){

 new CountDownTimer(10000, 2000) {       
    public void onTick(long millisUntilFinished) {          
     sound.stop();
     sound.start();        
    }       
   public void onFinish() {          
      repeatAlarm--;
      if(repeatAlarm > 0) wait30seconds(); 
    }   
 }.start();
}


 private void wait30seconds(){

  new Handler().postDelayed(new Runnable() {         
      @Override         
      public void run() {             
       playSoundFiveTimes();            
      }     
     }, 30000);
 }

Upvotes: 0

user unknown
user unknown

Reputation: 36269

for(i=0; i==5; ) {
    sound.start(); 
    if(sound.isPlaying()) {
        if(false) { 
            sound.stop(); 
            i++; 
        } 
    }
}    
sound.stop();

The second part of the for loop header is a condition which has to hold for the body to be entered. i=0 and i == 5 don't match, so the condition is wrong from the start and never entered.

You want to experess

 for(i=0; i!=5; ) {

or

 for(i=0; i<5; ) {

instead.

But where is i incremented or somehow else changed? In a block which is always false, in other words: Never.

Maybe you want to express that, if the statement before is false, do ...

You do so by negating the statement:

    if (! sound.isPlaying ()) { 
            sound.stop(); 
            i++; 
    }

Without knowing whether sound-playing is blocking or not it is hard to know how to control it.

Let's read the requirements and do it from scratch:

When you pressed a button, the application plays a sound lasting two seconds, five times. After playing the sound for the fifth time the application sleeps for thirty seconds. After sleeping, the cycle is repeated two more times.

You have an outer loop of 3 times, and an inner loop of 5 times.

for (int i=0; i < 3; ++i) {
    for (int j=0; j < 5; ++j) {
        sound.start(); 
        sleep (2); // seconds of sound
    } 
    sleep (30); // seconds of silence
}    

Upvotes: 2

Abhiram
Abhiram

Reputation: 1

Just keep a counter variable that will tell the application to sleep when it has reached 5, in your while loop. Pseudo code is something like this :

final int REPEAT_RATE = 5;
int counter = 0;
while(counter < 3 * REPEAT_RATE)
{
    if(counter == REPEAT_RATE) 
    {
        //Sleep for thirty seconds
    }
    //Here Play the sound for 2 seconds
}

Upvotes: 0

Related Questions