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.
From this state diagram, one can see that a MediaPlayer object has the following states: When a MediaPlayer object is just created using new or after reset() is called, it is in the Idle state; and after release() is called, it is in the End state. Between these two states is the life cycle of the MediaPlayer object.
MediaPlayer Class in Android is used to play media files. Those are Audio and Video files. It can also be used to play audio or video streams over the network.
The Media Player requires a SurfaceHolder object for displaying video content, assigned using the setDisplay() method. The Surface View is a wrapper around the Surface Holder object. Note that we must implement the SurfaceHoler. Callback interface.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With