[android-developers] Re: MediaPlayer sluggish to play sounds

2009-06-29 Thread Baratong

Actually what you described is exactly how I handle the audio in this
app. It's a video poker app but I preload all sounds and even on
payout sounds of the coins dropping I load 4 instances of it so they
can overlap while the game is in that mode. What I found was after 3
seconds of no audio at all the audio system goes to sleep. While I
would see in the log a message about xx millsecond delay on startup
again -- generally less than 100mseconds I still had the problem with
the sound I immediately tried playing to 'queue' instead of actually
playing...

On Jun 29, 2:13 pm, Paul Kilgo  wrote:
> On Sat, Jun 27, 2009 at 3:08 PM, Baratong  wrote:
>
> > What I ended up doing was this:
> > 1. Use Audacity to create a 1-second .wav of total silence and add the
> > wave into my manifest as a raw resource referenced in the app as
> > R.raw.silence.
> > 2. On startup, create a MediaPlayer instance, load the R.raw.silence
> > resource and play in a loop constantly.
>
> I'm new to the SDK and I did have some troubles with sounds, but I found
> what worked for me was to load all the sounds (at least the ones that will
> be used frequently) into their own MediaPlayer object using
> MediaPlayer.create(). Set each of the MediaPlayer objects to have an
> OnCompletionListener() to call MediaPlayer.seekTo(0) to reset the sound. You
> could extend the idea to store a bunch of MediaPlayer's in a hashmap if your
> situation called for it.
>
> Using this method I haven't really seen much latency at all in my sound
> effects. Does this method still cause the audio system to fall asleep during
> disuse? I didn't notice much at all.
>
> --
> --Paul
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: MediaPlayer sluggish to play sounds

2009-06-29 Thread Baratong

I actually tried that one first and found the soundpool to be a little
less than ready for primetime... it didn't work reliably in my testing
and even crashed from time to time. After that I went back to trying
to solve the MediaPlayer audio system wake-up issue. I'm building
under the Android 1.1 as I wanted my app to work on the widest
possibly pool of phones in the field. Is soundpool more reliable under
1.5?

On Jun 29, 2:10 pm, Mark Murphy  wrote:
> Baratong wrote:
> > I've found the audio subsystem is a little quirky. It's great if you
> > are playing songs, or video but if you are writing a game that plays
> > short sounds quickly it poses problems. After much work I found a
> > pretty cool solution to one of the annoying problems ofMediaPlayer
> > beingsluggishto play audio.
>
> An even simpler and better-performing solution is to use SoundPool,
> which was designed for use by "a game that plays short sounds quickly".
>
> Here's a presentation from Google I/O that, among other things, covers
> the differences betweenMediaPlayerand SoundPool:
>
> http://code.google.com/events/io/sessions/MasteringAndroidMediaFramew...
>
> --
> Mark Murphy (a Commons 
> Guy)http://commonsware.com|http://twitter.com/commonsguy
>
> Looking for Android opportunties?http://wiki.andmob.org/hado
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: MediaPlayer sluggish to play sounds

2009-06-29 Thread Baratong


In this code I merely place the sound in the 'pause' state as I simply
want to unpause with the 'start()'. Doing a start on a paused
MediaPlayer instance does not reload the sound but simply changes
state back to 'Started' which makes it begin playing again thus waking
up the audio system. (see MediaPlayer state diagram at:
http://developer.android.com/reference/android/media/MediaPlayer.html)

Stopping the sound would be wasteful as it places the audio in the
'stopped' state which requires a 'prepare()' call followed by a start
() to get it going again. I simply want it to start playing in order
to wake up the audio system quickly.

On Jun 29, 1:56 pm, Marco Nelissen  wrote:
> On Sat, Jun 27, 2009 at 1:08 PM, Baratong wrote:
>
> > I've found the audio subsystem is a little quirky. It's great if you
> > are playing songs, or video but if you are writing a game that plays
> > short sounds quickly it poses problems. After much work I found a
> > pretty cool solution to one of the annoying problems ofMediaPlayer
> > beingsluggishto play audio.
>
> > The problem is the audio system tends to go to sleep after 3 seconds.
> > When my application tries to play the next sound the phone's audio
> > system wakes up but seems to queue the sound instead of playing it.
> > When my application plays yet another sound -- as long as the sound
> > system hasn't gone back to sleep again -- it plays BOTH sounds at
> > once! This causes unacceptable behavior for any game.
>
> > What I ended up doing was this:
> > 1. Use Audacity to create a 1-second .wav of total silence and add the
> > wave into my manifest as a raw resource referenced in the app as
> > R.raw.silence.
> > 2. On startup, create aMediaPlayerinstance, load the R.raw.silence
> > resource and play in a loop constantly.
>
> > This worked great at keeping the audio system awake, however the phone
> > was now draining the battery quickly whenever the game was loaded.
> > Since Android (and most phones) don't have a great way for the user to
> > terminate a task, this meant if you played the game for a couple of
> > minutes in the morning, the battery would drain at an accelerated rate
> > for the rest of the day! A bit unacceptable.
>
> Indeed. It is unacceptable for an application to not clean up after
> itself. Why did you not simply stop the sound in your application's
> onPause() ?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] MediaPlayer sluggish to play sounds

2009-06-29 Thread Baratong


I've found the audio subsystem is a little quirky. It's great if you
are playing songs, or video but if you are writing a game that plays
short sounds quickly it poses problems. After much work I found a
pretty cool solution to one of the annoying problems of MediaPlayer
being sluggish to play audio.

The problem is the audio system tends to go to sleep after 3 seconds.
When my application tries to play the next sound the phone's audio
system wakes up but seems to queue the sound instead of playing it.
When my application plays yet another sound -- as long as the sound
system hasn't gone back to sleep again -- it plays BOTH sounds at
once! This causes unacceptable behavior for any game.

What I ended up doing was this:
1. Use Audacity to create a 1-second .wav of total silence and add the
wave into my manifest as a raw resource referenced in the app as
R.raw.silence.
2. On startup, create a MediaPlayer instance, load the R.raw.silence
resource and play in a loop constantly.

This worked great at keeping the audio system awake, however the phone
was now draining the battery quickly whenever the game was loaded.
Since Android (and most phones) don't have a great way for the user to
terminate a task, this meant if you played the game for a couple of
minutes in the morning, the battery would drain at an accelerated rate
for the rest of the day! A bit unacceptable.

So here's the rather simple solution: Create a runnable under the
thread's handler and have the runnable keep track of the audio system.
If no user action has happened in 15 seconds, then pause the silence
track allowing the audio system to go to sleep. On the next user
action, restart the silence which wakes up the audio system which
places the silence clip on the queue. The next sound played works
perfectly as the silence actually plays along with the sound you want!

So in the program define a handler:
// Setup our handler for this thread's runnables
private Handler MyThreadHandler = new Handler();

 Inside the applications activity in the onCreate method:
 mSilence = MediaPlayer.create( (Context)this, R.raw.silence);

 //Setup the audio sleep runnable with the following call
   SetupAudioSleepTimeouts();  // <-- this is OUR audio sleeper
(below)

Now, anywhere in the program an event happens that means there is user
interaction ongoing or you just aren't ready to let the audio go to
sleep yet add this:

 AudioSleepKeepAwake(); // Keep our audio system awake



 Later within our activity class add the audio support logic

 //
// audio control routines
//
private static final int AUDIOSLEEPTIME = 1000; // 1000 1 second
per audio timer tick
private static final int AUDIOSLEEPTIMEOUT = 15; // /timeout after
15 seconds of non activity and allow audio subsystem to goto sleep
private static final int AUDIOPAUSED = -1;
intAudioSleepTimer;// Counter to put audio to sleep.
If negative then we are paused.


//--
// AudioSleeperTask
//This runs all the time in the background
//and handles the 'silence' track that runs in the
background.
//  If no user activity occurs for XX seconds this task
stops the
//  background music and allows the
//
private Runnable AudioSleeperTask = new Runnable()
{
// Task to perform on each system tick
public void run()
{
if( AudioSleepTimer != AUDIOPAUSED )
{
AudioSleepTimer++;

if( AudioSleepTimer > AUDIOSLEEPTIMEOUT )
{
Log.i(TAG, "Audio: Putting audio to sleep");
mSilence.pause();
AudioSleepTimer = AUDIOPAUSED;
}
}

 MyThreadHandler.postDelayed( this, AUDIOSLEEPTIME );
}
};


//--
// Handle setting up the audio sleep timeout subsystem
public void SetupAudioSleepTimeouts()
{

Log.i(TAG, "Audio: Setup Audio KeepAwake");

// Get our silence started so we don't go to sleep
mSilence.setLooping( true );
mSilence.start();

// Setup the audio sleep timer
AudioSleepTimer = 0;
MyThreadHandler.removeCallbacks( AudioSleeperTask );
MyThreadHandler.postDelayed( AudioSleeperTask,
AUDIOSLEEPTIME);

}


//--
// Keep the audio sleep timer awake
//
public void AudioSleepKeepAwake(){

if( AudioSleepTimer == AUDIOPAUSED )
{
Log.i(TAG, "Audio: Waking up Audio");
mSilence.start();
}

AudioSleepTimer = 0;
}


Of cou