One way of loading a media resource is to directly instantiate a MediaResource and load the video content to be played.
Loading a MediaResource is an asynchronous multiple-step process.
This starts the resource loading process.
onPrepared
onStateChanged, and check for INITIALIZED and ERROR
This state indicates that the media has successfully loaded. Calling prepareToPlay starts the advertising resolution and placement process.
The following simplified sample code illustrates the process of loading a media resource:
// mediaResource is a properly configured MediaResource instance // mediaPlayer is a MediaPlayer instance // register a PlaybackEventListener implementation with the MediaPlayer instance mediaPlayer.addEventListener(MediaPlayer.Event.PLAYBACK, new MediaPlayer.PlaybackEventListener() { @Override public void onPrepared() { // at this point, the resource is successfully loaded and available // and the MediaPlayer is ready to start the playback // once the resource is loaded, the MediaPlayer is able to // provide a reference to the current "playable item" MediaPlayerItem playerItem = mediaPlayer.getCurrentItem(); if (playerItem != null) { // here we can take a look at the properties of the // loaded stream } } @Override public void onStateChanged(MediaPlayer.PlayerState state, MediaPlayerNotification notification) { if (state == MediaPlayer.PlayerState.ERROR) { // something bad happened - the resource cannot be loaded // details about the problem are provided via the MediaPlayerNotification instance }else if (state == MediaPlayer.PlayerState.INITIALIAZED) { mediaPlayer.prepareToPlay(); } } // implementation of the other methods in the PlaybackEventListener interface ... }