Android:Media Player difference between PrepareAsync() and Prepare()

user3726986 picture user3726986 · Aug 22, 2014 · Viewed 9.4k times · Source

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 .

Answer

cliffroot picture cliffroot · Aug 22, 2014

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.