pgorbas
unread,Feb 4, 2011, 4:45:49 PM2/4/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to vogella
OK I solved my sound problem.
The issue was not with the emulator not making sound ( as verified by
the fact it would not work on my device either ), but a unaccounted
problem with the SoundPool class. I looked in the logcat and found
references like "sample 1 not ready". Researching this I finally
found a obscure thread in which it was mentioned that it takes some
time before sound pool is ready to be used ( and therefore all sounds
should be loaded well before they are used ).
I modified the playSound method to monitor the return value of the
soundPool.play(...) method call. It returns the id of the running
sound stream, or 0 if it failed ( i.e. "sample 1 not ready" ).
What I did was to put it into a loop, and when the return value of the
soundPool.play(...) method call was 0, I had the thread sleep for 1
millisecond, then try again. With this method in place, I now always
get a sound.
As a side note I have also been running these tutorials with my SDK
set up for Android 2.2 instead of Android 2.3.1, because Android 2.2
is what is installed on my device, a Sprint LG optimus S LS670.
I have ran my modified code on both a Android 2.2 and a Android 2.3.1
Virtual device.
When I run the code on the older Android 2.2 VD, it typically took
about 10 to 15 loops ( so a 10 to 15 ms delay ) before the sounspool
was ready to play the sound.
When I ran the same code on a Android 2.3.1 VD, the delay was MUCH
worse, taking around a 350 ms delay before it would play - yes almost
35 times slower!
When I ran the same code deplyed to my device running Android 2.2, the
time delap was about identical to running it on the emulator.
Hope this helps people!
Here is my modified code:
public void playSound(View view)
{
// First parameter defines the number of channels which should be
played
// in parallel, last one currently not used
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
int soundID = soundPool.load(this, R.raw.sound1, 1);
// Getting the user sound settings
AudioManager audioManager = (AudioManager)
getSystemService(AUDIO_SERVICE);
float actualVolume = (float)
audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float)
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
int streamId = soundPool.play(soundID, volume, volume, 1, 0, 1f);
for( int i = 0; i<1000; i++)
{
if (streamId != 0 )
{
Toast.makeText( this
, "I should be playing a
sound! ( on on try "+i+" )"
, Toast.LENGTH_SHORT
).show();
break;
}
else
{
try
{
Thread.sleep(1);
streamId = soundPool.play(soundID, maxVolume, maxVolume, 1, 0,
1f);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
Toast.makeText( this
, "Thread sleep
exception =======>\n"+e+"\n================"
, Toast.LENGTH_LONG
).show();
e.printStackTrace();
}
}
}//for( int i = 0; i<10; i++)
}//public void playSound(View view)