0

Hi I am new in android programming. My problem is I want to make a delay before continuing the next loop over and over again depending on the size of my Array so that the sounds will not overlap.

Here is my code:

protected void managerOfSound() {
    int size = tempq.size();
    for (int i = 0; i < tempq.size(); i++) {
        String u =tempq.get(i);

        //WHOLE
        if (u.equals("a4")){
            mp = MediaPlayer.create(this, R.raw.a4);
            mp.start();


        }else if (u.equals("b4")){
            mp = MediaPlayer.create(this, R.raw.b4);
            mp.start();


        }else if (u.equals("c4")){
            mp = MediaPlayer.create(this, R.raw.c4);
            mp.start();


        }
    }
}

I tried using the Thread.sleep but I need to stop playing the sounds using a button click but i cannot click a button because of Thread.sleep so I have to disregard using it. But here is my code for that in case anyone have a solution for that. I've already tried the Thread.currentThread.interrupt but my sounds are overlapping again when i use that.

Code using Thread.sleep:

protected void managerOfSound() {
    int size = tempq.size();
    for (int i = 0; i < tempq.size(); i++) {
        String u =tempq.get(i);

        //WHOLE
        if (u.equals("a4")){
            mp = MediaPlayer.create(this, R.raw.a4);
            mp.start();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ie) {
                // TODO Auto-generated catch block

            }

        }else if (u.equals("b4")){
            mp = MediaPlayer.create(this, R.raw.b4);
            mp.start();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ie) {
                // TODO Auto-generated catch block

            }

        }else if (u.equals("c4")){
            mp = MediaPlayer.create(this, R.raw.c4);
            mp.start();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ie) {
                // TODO Auto-generated catch block

            }

        }
    }
}

Another way that I tried is the postDelayed but it seems like it is not Delaying after the first loop.

Here is my code for postDelayed:

protected void managerOfSound() {
    int size = tempq.size();
    for (int i = 0; i < tempq.size(); i++) {
        String u =tempq.get(i);

        //WHOLE
        if (u.equals("a4")){
            mp = MediaPlayer.create(this, R.raw.a4);

            handler.postDelayed(new Runnable(){
               @Override
               public void run() {  
                   mp.start();
               }            
            },4000);

        }else if (u.equals("b4")){
            mp = MediaPlayer.create(this, R.raw.b4);

            handler.postDelayed(new Runnable(){
               @Override
               public void run() {  
                   mp.start();
               }            
            },4000);

        }else if (u.equals("c4")){
            mp = MediaPlayer.create(this, R.raw.c4);

            handler.postDelayed(new Runnable(){
               @Override
               public void run() {  
                   mp.start();
               }            
            },4000);                
        }
     }
  }

I also tried changing ,4000 to (i+1)* 4000 as what others suggested but still its not working. And I also Tried Putting the Delay Outside the if else statements but the Result is just the same.

Is there anyone who can suggest and help me with my problem? Please. I've been working with for a week. Thanks in advance.

ALD
  • 23
  • 2
  • 11
  • create a new thread and use sleep, do not use main thread – Xcihnegn Mar 12 '15 at 08:03
  • 1
    NEVER EVER use `Sleep` in any programming language or on any platform - it's evil - especially when it comes to Android. – Squonk Mar 12 '15 at 08:06
  • I already tried but it's still the same. i cannot click the button for stop. – ALD Mar 12 '15 at 08:10
  • others suggested me to use postDelayed but its not delaying again after it has delayed on the first time. – ALD Mar 12 '15 at 08:11
  • @ALD : The correct way to do what you want to do, i.e., wait for completion of each sound in a sequence of sounds, is to use the `OnCompletion(..)` listener of the `MediaPlayer` class. Don't use a `for` loop, simply use an int which starts each sound and in the `onCompletion(...)` callback, increment the int (unless it is >= of the upper range) and then do what is necessary to start the `MediaPlayer` with the next sound (based on the int value). – Squonk Mar 12 '15 at 08:59

2 Answers2

1

Please use this

  for (int i = 0; i < 4; i++) {
            String u = " q";

            handler = new Handler();
            handler.postDelayed(new Runnable(){
                @Override
                public void run() {  
                   Toast.makeText(getApplicationContext(), "i" , Toast.LENGTH_SHORT).show();
                }            
             },4000);
}

Means Put all your condition in only one handler and initialise handler every time. This is properly working at my end.

Amarjit
  • 4,327
  • 2
  • 34
  • 51
1

Try this code:

 for (int i = 0; i < tempq.size(); i++) {
                String u =tempq.get(i);

                //WHOLE
                if (u.equals("a4")){
                    mp = MediaPlayer.create(this, R.raw.a4);
                    mp.start();


                }else if (u.equals("b4")){
                    MediaPlayer mp1 = MediaPlayer.create(this, R.raw.b4);
                    mp.setNextMediaPlayer(mp1);


                }else if (u.equals("c4")){
                    MediaPlayer mp2= MediaPlayer.create(this, R.raw.c4);
                    mp.setNextMediaPlayer(mp2);


                }
            }
Pratik Popat
  • 2,891
  • 20
  • 31
  • thanks i tried it and it worked but what if i have set of random array like for example a4,a4,c4,b4,a4,b4 depending on the user input. – ALD Mar 12 '15 at 08:53