I wanted to implement basic media player functionality and was confused between PrepareAsync() and Prepare() method calls. Which one should be used if the audio file is in the raw folder .
-
1Both do the same thing at the end, but Prepare() will not return until preparation is done, while PrepareAsync will return immediately and do the preparation in the background.– WildcopperAug 22, 2014 at 11:59
-
It means Suppose I have audio clip of say for example 10 min . . PrepareAsync will return audio for first few seconds immediately and prepare might take a bit longer to get the file read but once its done it will return the object for the whole file ?– user3726986Aug 22, 2014 at 12:02
-
You can't start playing before the preparation is done or you will have an exception. With PrepareAsync() you will have to start the playback yourself in the "onPrepared" method of the onPreparedListener.– WildcopperAug 22, 2014 at 15:40
2 Answers
The difference between those methods is basically in what thread they're executed.
Prepare
runs in the thread you call it (most frequently UI thread) and thus if it takes long time (buffering video from the Internet and such) it will block your UI thread and a user might get ANR.
PrepareAsync
runs in a background thread and thus UI thread is not blocked as it returns almost immediately. But the player is not prepared so you want to set onPreparedListener
in order to know when the MediaPlayer
is ready to be used.
-
but if the use case is basic which in my case is (i.e read from raw folder resource ) I can go for Prepare() method. It would work faster in this case ?– user3726986Aug 22, 2014 at 12:04
-
1it'll probably work a little faster (though I doubt if you notice it). you may try yourself and see if it's blocking your UI, if not you can go for
prepare
method, but generally it'd be better to useprepareAsync
as it won't generate problems in future. Aug 22, 2014 at 12:06
prepare() method is generally used when we want to play our media file synchronously. prepareAsync() is generally used when we want to play asynchronously.
for eg:
mediaplayer.prepare()
It is used to play the file from local media resources.
mediaplayer.prepareAsync() is generally used for playing the live data over stream. It allows to play without blocking the main thread.
If we use prepare()
for live data streaming it eventually crashes because the data is received in streams.
Basically what prepare()
does it first load all the data and then it play. Thus it allows to play the media file synchronously.
And prepareAsync()
play the data whatever it has in its buffer.
Here is the final Quotes
here are two ways (synchronous vs. asynchronous) that the Prepared state can be reached: either a call to prepare() (synchronous) which transfers the object to the Prepared state once the method call returns, or a call to prepareAsync() (asynchronous) which first transfers the object to the Preparing state after the call returns (which occurs almost right way) while the internal player engine continues working on the rest of preparation work until the preparation work completes. When the preparation completes or when prepare() call returns, the internal player engine then calls a user supplied callback method, onPrepared() of the OnPreparedListener interface, if an OnPreparedListener is registered beforehand via setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener).
The main difference is that when we use files then we call prepare() and when we use streams then we call prepareAsync().
In your case it must be prepare() method
Check prepareAsync() and prepare() refer the docs its clearly stated