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

前端 未结 2 1949
深忆病人
深忆病人 2021-01-11 14:55

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 t

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-11 15:22

    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.

提交回复
热议问题