suku suku - 3 months ago 32
Android Question

What is the difference between mediaplayer.create() and mediaplayer.prepareAsync()?

I have used the following code:

mp = MediaPlayer.create(this, Uri.parse("file://"+filePath));
mp.start();


This works fine. Then I wanted to play music from a folder

mp.setDataSource(this, Uri.parse("file://"+filePath));
mp.prepareAsync();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});


Are there any performance differences between the two method?

Answer

Sure, create method inits object in main thread. So code lines below it should wait for create.

On the other hand, prepare asynchronous opens a new thread to init object then notify you to run next operations while main thread run other lines.

Edit: As @CommonWares mentioned in the comment, mp.create() is a convenient method of calling mp.setDataSoucer() + mp.prepare() at the same time

Comments