Is the Mediaplayer.create() method called in the background?

Asked

Viewed 97 times

3

I was reading the documentation of Android Media Playback and a doubt occurred to me. In the documentation it said that it is not advisable to call the method mediaPlayer.prepare() in thread responsible for UI, and would like to know if the method MediaPlayer.create(this, R.raw.amostra_de_audio) is called in the background or some additional configuration is required. Thanks in advance.

1 answer

2


The method MediaPlayer.create() is not asynchronous. It could not be because it returns an instance of Mediaplayer.
He internally calls the method prepare() thus returning the Mediaplayer ready-to-use.

To achieve this in an asynchronous way, an instance must be created, through new and call the method prepareAsync().

MediaPlayer myMediaPlayer = new MediaPlayer();
....
....
myMediaPlayer.setDataSource(url);
myMediaPlayer.prepareAsync();

myMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

    @Override
    public void onPrepared(MediaPlayer player) {
        player.start();
    }

});

The method onPrepared() of Onpreparedlistener passed to the method setOnPreparedListener() will be called when the Mediaplayer is ready to be used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.