Android Sounds - Tutorial - NO SOUND from emulator

7,343 views
Skip to first unread message

paul gorbas

unread,
Feb 2, 2011, 6:23:19 PM2/2/11
to vogella
I can compile and run the application in the tutroail Android Sounds -
Tutorial, but my emulator is not producing sounds of any kind.

I tried putting the command line option "-audio oss" only to get ther
error "- Emulator] ko:'oss' is not a valid audio output backend"

Please detail how to get sounds to work in the emulator!

Lars Vogel

unread,
Feb 3, 2011, 2:46:33 AM2/3/11
to vog...@googlegroups.com
I think such a thing it better test on a real device.

2011/2/3 paul gorbas <pgo...@yahoo.com>:

> --
> You received this message because you are subscribed to the Google Groups "vogella" group.
> To post to this group, send email to vog...@googlegroups.com.
> To unsubscribe from this group, send email to vogella+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/vogella?hl=en.
>
>

--
Lars
http://www.vogella.de - Tutorials about Java, Eclipse and Web programming
http://www.twitter.com/vogella - Lars on Twitter

pgorbas

unread,
Feb 3, 2011, 11:32:24 PM2/3/11
to vogella
It still would be a good thing to be able to hear sounds though the
emulator - can ANYONE hear ANY sounds though their emulator? Mayby
it's a windows 7 64 bit incompatbility thing, if is it is not
documented.

I did try the application on my android phone.

I did modify the code so that I get a Toast when it should be playing
a sound.

Running on emulator or on my device produces the same rsult, I get my
Toast, but no sound.

Lars Vogel

unread,
Feb 4, 2011, 12:51:38 AM2/4/11
to vog...@googlegroups.com
Hi,

I suggest to use the Android Google Group to find out.

Best regards, Lars

2011/2/4 pgorbas <pgo...@yahoo.com>:

pgorbas

unread,
Feb 4, 2011, 4:45:49 PM2/4/11
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)

Russell Bateman

unread,
Feb 4, 2011, 5:14:26 PM2/4/11
to vog...@googlegroups.com, michael....@hp.com
Copious thanks for this information, Paul.

Best regards,

Russ Bateman

Lars Vogel

unread,
Jun 16, 2011, 4:04:21 AM6/16/11
to vog...@googlegroups.com
Hi,

sorry for the long delay. As of API8 you can check if the loading is complete. I updated http://www.vogella.de/articles/AndroidMedia/article.html

Best regards, Lars

2011/2/4 pgorbas <pgo...@yahoo.com>
OK I solved my sound problem.
--
You received this message because you are subscribed to the Google Groups "vogella" group.
To post to this group, send email to vog...@googlegroups.com.
To unsubscribe from this group, send email to vogella+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/vogella?hl=en.

Reply all
Reply to author
Forward
0 new messages