Asynchronous prepare

suggest change

MediaPlayer.prepare() is a blocking call and will freeze the UI until it completes. To avoid that, use MediaPlayer.prepareAsync().

mMediaPlayer = ... // Initialize it here
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
    @Override
    public void onPrepared(MediaPlayer player) {
        // Called when the MediaPlayer is ready to play
        mMediaPlayer.start();
    }
}); // Set callback for when prepareAsync() finishes
mMediaPlayer.prepareAsync(); // Prepare asynchronously to not block the Main Thread

On synchronous operations, errors are normally signaled with an exception or an error code. With asynchronous prepare, register an error listener so the application is notified. For MediaPlayer:

mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener(){
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            // ... react appropriately ...
            // The MediaPlayer has moved to the Error state, must be reset!
            // Return true if the error has been handled.
            return true;
        }
});

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents