"is the CPU pegged?", and friends

Lessons I learnt today

is the CPU pegged?

When is the CPU pegged? message shows up in the LogCat console tab, it means you are not filling in quickly enough the audio buffer for an AudioTrack. Or that it seems so. I happened to locate where the message is in Android's source code, but since I am not familiar at all with it, I am just doing a quick guess which was pseudo-confirmed when the messages stopped as soon as I began refilling quicker the audio buffer.

Thread.stop is deprecated - and it will be ignored!

Docs say:

This method is deprecated. because stopping a thread in this manner is unsafe and can leave your application and the VM in an unpredictable state.

As to why is that deprecated on the basis that using it can cause stability issues, I am afraid I can't elucidate the answer yet.

I am using an strange solution where I add a method called doStop to the class that extends Thread, with the intention that it sets an internal boolean bar (mIsRunning) to false in that case, and returns from its run method as soon as mIsRunning is false. The reason why I am using this is because I saw that in an SDK tutorial, but still feels a bit strange.

Incidentally I also took the opportunity to refresh my mind regarding Java threads, which had been much forgotten.

Sometimes LogCat stops reporting messages

Close and reopen the emulator and Eclipse and it will be sorted out.

Things I still need to investigate

  • Need to find out a better way to determine if a buffer needs audio data. I am missing some kind of callback, SDL style. Currently I am just pushing audio data using a thread which sleeps for a certain amount of time, but I sometimes get AudioFlinger - write blocked for xxx ms messages if the buffer is not sufficiently empty
  • What's more efficient and in which cases, regarding the use of integers, shorts, floats... I am using a LookUpTable for storing precalculated Math.sin values but I am unsure if I am optimizing too early...
  • Would love to find more AudioTrack examples and documentation. There's an impressive scarcity of resources about AudioTrack while there's a cornucopia of samples for MediaPlayer, JetPlayer and etc. AudioTrack must be feeling discriminated: there is not even an AudioTrack sample program in the official SDK samples!

Edit: added the link to Java Threads tutorial which I forgot to add.