Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 .

like image 389
user3726986 Avatar asked Aug 22 '14 11:08

user3726986


People also ask

What is MediaPlayer prepare?

The MediaPlayer$prepare() is a blocking call and will freeze the UI till execution completes. To solve this problem, MediaPlayer$prepareAsync() can be used.

Which method is used to bring the MediaPlayer to prepared state?

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 ...

How do I reset my MediaPlayer on Android?

Just use pause to stop, followed by seekTo(0) before restarting: mediaPlayer. seekTo(0); mediaPlayer.


1 Answers

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

like image 198
Akshay Mukadam Avatar answered Sep 24 '22 06:09

Akshay Mukadam