The tutorial here explains that a service actually uses the main thread. So it uses prepareAsync
to avoid blocking UIS:
http://developer.android.com/guide/topics/media/mediaplayer.html#asyncprepare
I was wondering where the async callback onPrepared
runs. In the example onPrepared
calls start of MediaPlayer
. Is start also a CPU-intensive method? If it runs in the same thread, it also blocks.
MediaPlayer.start()
is not an intensive operation in the least. The MediaPlayer
uses its own native thread to perform tasks, but a call to the synchronous prepare
method can take too long for the UI thread, particularly if it is remote media you are trying to play. In that case it has to wait for one or more network requests, data to buffer, etc. The onPrepared
callback will happen on the main thread if that is where you called prepareAsync
(or whatever thread you called it from, to be more precise).