Looks like you're blocking on the main UI thread which is a no-no.

Look at the PostDelayed method of Handler. That is, just make
pgs.playSound(2) into the body of a Runnable and the random time
and use them for the PostDelayed  parameters.

Mike

On 03/30/2010 02:28 PM, BiiG wrote:
Hi,
I have an activity that has one button. When the user presses the
button, a wav file is played with soundpool. Then after a random
number of seconds from 1 to 4 has passed using Thread.sleep(), another
wav file is played. Everything works as it should on the emulator. But
when I go to test it on an actual phone (an htc MyTouch) I get an
Activity Not Responding dialog with Wait and Force Close buttons. Even
though the sounds play as expected and everything works on the phone,
I still get this stupid dialog.

If I set the sleep time to less than 5 seconds, everything works...
the problem is that the first sound clip is 5 seconds long. So at a
minimum, I have to wait longer than 5 seconds from when I play sound 1
until I call sound 2.

Below is the bit of code doing the work... pgs is my soundpool class
from posts in this forum.
                 Random rn = new Random();
        long range = 5;
        long fraction = (long) (range*rn.nextDouble());
        int randomNumber = (int)(fraction)*1000+5000;
        pgs.playSound(1);
        try {Thread.sleep(randomNumber);} catch (InterruptedException e)
{}
        pgs.playSound(2);

I've tried using System.currentTimeMillis() and looped until the
difference between when I called the first sound and the current time
is larger than the random time. And in the loop I incremented a dummy
counter just to give the activity something to do. Yet if the time is
larger than 5 seconds, this method does the same thing and the
Activity Not Responding dialog is shown.

I can, and have, created an individual wav file with both sounds for
several pseudo random lengths, but this drastically increases the size
of my final apk.

PLEASE HELP!!!

Thanks
Mike


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe from this group, send email to 
android-developers+unsubscribegooglegroups.com or reply to this email with the words 
"REMOVE ME" as the subject.

Reply via email to