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

3

I was reading the Android Media Playback documentation and I had a question. The documentation said that it is not advisable to call the mediaPlayer.prepare() method in the thread responsible for the UI, and would like to know if the MediaPlayer.create(this, R.raw.amostra_de_audio) method is called in the background or is needed some additional configuration. Thank you very much in advance.

    
asked by anonymous 26.12.2015 / 15:53

1 answer

2

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

In order to achieve the same asynchronously you will have to create an instance, using new and call the prepareAsync() method.

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

myMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

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

});

The% method of the OnPreparedListener passed to the onPrepared() method will be called when the MediaPlayer is ready to be used.     

26.12.2015 / 16:35